我找到了以下功能,但我不知道为什么__bases__
需要额外的扫描:
def getMembersWithBases(classType):
members = set(dir(classType))
# recursive bases scan
for baseClassType in classType.__bases__:
members.update(getMembersWithBases(baseClassType))
return members
下面的函数更快并且给出相同的结果——那么为什么还__bases__
需要额外的扫描呢?
def getMembers(classType):
members = set(dir(classType))
return members
一些带有新式和旧式类的测试代码:
class Father(object):
def testFather():
pass
class Mother(object):
def testMother():
pass
class Child(Father, Mother):
def testChild():
pass
print type(Child)
print getMembers(Child) == getMembersWithBases(Child)
class Father:
def testFather():
pass
class Mother:
def testMother():
pass
class Child(Father, Mother):
def testChild():
pass
print type(Child)
print getMembers(Child) == getMembersWithBases(Child)
结果:
<type 'type'>
True
<type 'classobj'>
True