python - 对象在动态类型创建期间被子类化,但在 python2 中的经典类定义期间不被子类化

标签 python python-3.x python-2.7 class

我们知道这会创建一个类:

class X:
    a = 1

并且,在 Python 3 中,due to new style classes , X自动继承自 object ,但在 Python 2 中,它不会:

>>> X.__bases__ # Python 2
()
>>> X.__bases__ # Python 3
(<class 'object'>,)

we also know我们可以使用类型工厂动态创建这样一个类:

X = type("X",(object,), {"a":1})
      name^   ^bases     ^ "class" body

但是,如果我们省略 objectbases 元组中,就像我们对 class 所做的一样语法,我们继承自 object在 python3 中,出乎意料的是,在 python2 中也是如此:

X = type("X", ( ), {"a":1})
               ^ empty bases tuple
>>> X.__bases__ # Python 2
(<type 'object'>,)
>>> X.__bases__ # Python 3
(<class 'object'>,)

我预计 X.__bases__在 python 2 中为空。
而且这不是一个记录在案的功能,我在互联网上几乎找不到相关信息。

事实上,python's official documentation矛盾的是:

the bases tuple itemizes the base classes and becomes the __bases__ attribute

但是,如上所示,()仍然导致 (<type 'object'>,)在 Python 2 中,所以它变成 __bases__ 并不是真的属性。

谁能解释这种行为?

最佳答案

让我们看看这个 Python 2 shell。

>>> class X1:
...     a = 1
... 
... X2 = type('X2', (), {'a': 1})
>>> type(X1)
0: <type 'classobj'>
>>> type(X2)
1: <type 'type'>
>>> import types
>>> assert types.ClassType is type(X1)

types.ClassType 被描述为:

The type of user-defined old-style classes.

基本上type在新型类的默认元类中。如果你想要旧式的元编程,你可以使用 types.ClassType以同样的方式。

>>> X3 = types.ClassType('X3', (), {'a': 1})
>>> X3.__bases__
2: () 

供引用,摘自 Python 2 的 New-style and classic classes :

Classes and instances come in two flavors: old-style (or classic) and new-style.

Up to Python 2.1 the concept of class was unrelated to the concept of type, and old-style classes were the only flavor available. For an old-style class, the statement x.__class__ provides the class of x, but type(x) is always <type 'instance'>. This reflects the fact that all old-style instances, independent of their class, are implemented with a single built-in type, called instance.

New-style classes were introduced in Python 2.2 to unify the concepts of class and type. A new-style class is simply a user-defined type, no more, no less. If x is an instance of a new-style class, then type(x) is typically the same as x.__class__ (although this is not guaranteed -- a new-style class instance is permitted to override the value returned for x.__class__).

关于python - 对象在动态类型创建期间被子类化,但在 python2 中的经典类定义期间不被子类化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56455247/

相关文章:

python - 如何退出程序 : sys. stderr.write() 或打印

python - 在同时安装了 python 2.7 和 3.5 的 Windows 上使用 pip

python - QTableView 选择改变

google-app-engine - Google App Engine python 2.7 中出现 "Release unlocked lock"错误,threadsafe=No

Python 何时使用 OOP 何时不使用 OOP

python - 如何通过正则表达式模式拆分字符串并捕获模式内容?

python - 将 float 转换为 int 并在四舍五入的情况下引发异常

python - 如何在Python中获取输入的特定部分以及多段落输入

python - 如何使用 numpy 使 for 循环更快

python-2.7 - Flask 中的 http header 在 Linux 服务器上不起作用