Python:可以在构建类型时调用

系统教程 行业动态 更新时间:2024-06-14 16:57:40
Python:可以在构建类型时调用__subclasses __(),而不是自定义对象(Python: can call __subclasses__() on build in types, not on custom object) class Test: pass print(Test.__subclasses__())

收益:

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.

更多推荐

本文发布于:2023-04-13 12:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/0351d0ec639d15e3def73b552c11c014.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时调   类型   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!