我无法在另一个类中调用一个类的函数

2024-04-18 22:39:01 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的代码。我无法在我的Demo类中调用google_charts类的函数。帮我解决这个问题。在

class google_charts:
    def all_files_to_one():
        pass

    prods = []
    srcs = []
    sname = []
    def process_compiled():
        global prods, srcs, sname
        sname.append('something')
        prods.append('something')
        srcs.append('something')

def demo():
    google_charts.all_files_to_one()
    google_charts.process_compiled()

demo()

输出:

^{pr2}$

Tags: todemodefgooglefilesallprocessone
3条回答

您要使用:

self.all_files_to_one()

除此之外:“self”参数丢失

这个问题实际上是基本的Python面向对象编程。在

请先阅读Python教程,而不是试错编程。在

所以不能代替阅读基本文档。在

google_contacts类中所有方法的签名都不正确。它们必须包含self参数。请看一下Python类的文档:

http://docs.python.org/tutorial/classes.html

这也可能有用:

What is the purpose of self?

在python中定义类时,其中的任何方法都必须将“self”作为第一个参数:

class cheese(object):
       def some_method(self):
           return None

相关问题 更多 >