我们知道这会创建一个类:
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
但是,如果我们省略 object
在 bases 元组中,就像我们对 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 oftype
, and old-style classes were the only flavor available. For an old-style class, the statementx.__class__
provides the class of x, buttype(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, calledinstance
.New-style classes were introduced in Python 2.2 to unify the concepts of
class
andtype
. A new-style class is simply a user-defined type, no more, no less. If x is an instance of a new-style class, thentype(x)
is typically the same asx.__class__
(although this is not guaranteed -- a new-style class instance is permitted to override the value returned forx.__class__
).
关于python - 对象在动态类型创建期间被子类化,但在 python2 中的经典类定义期间不被子类化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56455247/