查找特定类的实例

时间:2018-05-21 03:11:59

标签: python

假设以下代码:

class Person():
    def __init__(self,name):
        self.name = name

class Student(Person): pass

class Teacher(Person): pass

John = Student('John')
Tom = Student('Tom')
Victor = Teacher('Victor')

运行脚本后,类Person通过

了解其子类
In [22]: Person.__subclasses__()
Out[22]: [__main__.Student, __main__.Teacher]

如何找到类Student的所有实例,如:

Student.__instances__()
AttributeError: type object 'Student' has no attribute '__instances__'

0 个答案:

没有答案