在python中从另一个类调用一个类的函数
假设我有以下代码在python中从另一个类调用一个类的函数,python,class,Python,Class,假设我有以下代码 class ter: def func1() def func2() class fg: def gl1() def gl2() ifTrue) ter.func1() # func1 from class ter 如何从类fg调用类ter的func1?ter.func1()不起作用。因为ter是类的名称,ter.func1()是调用or()的语法。一般来说,你不应该仅仅为了组织函数而使用Python中的类;相
class ter:
def func1()
def func2()
class fg:
def gl1()
def gl2()
ifTrue)
ter.func1() # func1 from class ter
如何从类fg调用类ter的func1?ter.func1()不起作用。因为
ter
是类的名称,ter.func1()
是调用or()的语法。一般来说,你不应该仅仅为了组织函数而使用Python中的类;相反,您将使用“自由函数”或模块级函数
如果您实际拥有类ter
的实例,则可以调用该名称上的函数:
#对Person对象调用“实例方法”
class Person:
def __init__(self, name): # Constructor
self.name = name
def sayHello(self): # Class method (requires `self` parameter)
print 'Hello, {0}'.format(self.name)
def main():
p = Person('Joe') # Instantiate `Person` class (calls constructor)
p.sayHello() # Call an "instance method" on Person object
如果确定要使用静态方法,请执行以下操作:
class Person:
def __init__(self, name): # Constructor
self.name = name
def sayHello(self): # Class method (requires `self` parameter)
print 'Hello, {0}'.format(self.name)
@staticmethod
def makePerson(name): # Static Method (note, no `self` parameter)
p = Person(name)
return p
@classmethod
def makePerson2(cls, name): # Class method. First parameter is class
p = cls(name) # Call constructor for that class
return p
def main():
p2 = Person.makePerson('Joe') # Call static "factory" method
p2.sayHello()
最后,尽管Python没有大括号,但它对语法非常挑剔。如果不打算声明方法体,则必须使用:
由于
ter
是类的名称,因此ter.func1()
是调用or()的语法。一般来说,你不应该仅仅为了组织函数而使用Python中的类;相反,您将使用“自由函数”或模块级函数
如果您实际拥有类ter
的实例,则可以调用该名称上的函数:
#对Person对象调用“实例方法”
class Person:
def __init__(self, name): # Constructor
self.name = name
def sayHello(self): # Class method (requires `self` parameter)
print 'Hello, {0}'.format(self.name)
def main():
p = Person('Joe') # Instantiate `Person` class (calls constructor)
p.sayHello() # Call an "instance method" on Person object
如果确定要使用静态方法,请执行以下操作:
class Person:
def __init__(self, name): # Constructor
self.name = name
def sayHello(self): # Class method (requires `self` parameter)
print 'Hello, {0}'.format(self.name)
@staticmethod
def makePerson(name): # Static Method (note, no `self` parameter)
p = Person(name)
return p
@classmethod
def makePerson2(cls, name): # Class method. First parameter is class
p = cls(name) # Call constructor for that class
return p
def main():
p2 = Person.makePerson('Joe') # Call static "factory" method
p2.sayHello()
最后,尽管Python没有大括号,但它对语法非常挑剔。如果不打算声明方法体,则必须使用: 这应该打印
I am func1
。
这里要记住的一点是,除非调用静态方法,否则必须创建类的实例
这应该打印I am func1
。
这里要记住的一点是,除非调用静态方法,否则必须创建类的实例。请发布特定的错误消息。此代码错误(不是Python)我收到此错误消息异常。TypeError:func1()正好接受0个参数(给定1)@user2673943读取我的答案。调用实例方法总是需要一个参数(通常称为<代码>自身>代码> Python中,就像C++中的代码< < /C> >,C语言等),语法也有各种各样的问题。如果不打算声明方法体,则必须使用
pass
关键字。不起作用“
是不够的。请发布特定的错误消息。此代码错误(不是Python)我收到此错误消息异常。TypeError:func1()正好接受0个参数(给定1)@user2673943读取我的答案。调用实例方法总是需要一个参数(通常称为<代码>自身>代码> Python中,就像C++中的代码< < /C> >,C语言等),语法也有各种各样的问题。如果您不打算声明方法体,则必须使用pass
关键字。感谢您向我详细解释。感谢您向我详细解释。@user2673943,我严重怀疑它。@user2673943,我严重怀疑它。