Python 父对象中的self.\u dict\u\dict没有子属性?
我有两门课:Python 父对象中的self.\u dict\u\dict没有子属性?,python,inheritance,Python,Inheritance,我有两门课: class Base(object): def __init__(self): object.__init__(self) def print_methods(self): print self.__dict__ class Child(Base): def __init__(self): Base.__init__(self) def another_method(self): pass 现在我可以在子实例中调用pr
class Base(object):
def __init__(self):
object.__init__(self)
def print_methods(self):
print self.__dict__
class Child(Base):
def __init__(self):
Base.__init__(self)
def another_method(self):
pass
现在我可以在
子实例中调用print\u方法
,并希望看到另一个\u方法
。但是失败了。这与继承毫无关系Child。另一个方法()
是类的属性,而不是实例,因此它不在self
的目录中,而是在Child
目录中。如果仅创建Base
的实例并在此实例上调用print\u methods()
,则也不会看到print\u methods
要查找实例的所有方法,可以使用dir()
或inspect.getmembers()
(可以与callable()
结合使用,仅包括可调用属性)