收益:
AttributeError: class Test has no attribute '__subclasses__'和
print(int.__subclasses__())收益:
[<type 'bool'>]为什么我不能在自定义对象上调用子类 ()?
这种构建方法不适用于自定义类型,是吗?
class Test: pass print(Test.__subclasses__())每个类都保留一个对其直接子类的弱引用列表。 该方法返回所有这些仍然存在的引用的列表。 例:
returns:
AttributeError: class Test has no attribute '__subclasses__'And
print(int.__subclasses__())returns:
[<type 'bool'>]Why can't I call subclasses() on my custom object?
This build in methods aren't reserver for custom types, are they?
Each class keeps a list of weak references to its immediate subclasses. This method returns a list of all those references still alive. Example:
最满意答案
你需要这样做: -
class Foo(object): pass # This works perfectly fine now. print(Foo.__subclasses__())上面的类是一个“新风格”的类,因为它继承了对象类。 新式类提供了许多“旧式”类没有的额外框架。 新样式类的一个特定属性是能够使用子类方法确定类的子类 。
You will need to do this:-
class Foo(object): pass # This works perfectly fine now. print(Foo.__subclasses__())The class above is a "new-style" class because it inherits from the object class. New-style classes provide a lot of extra framework that "old-style" classes do not have. One particular attribute of a new-style class is to be able to determine the subclasses of the class with the subclasses method.
更多推荐
发布评论