[英]Python: can call __subclasses__() on build in types, not on custom object
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:
例子:
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.
新式类的一个特殊属性是能够使用subclasses方法确定类的子类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.