为什么python2和python3中的代码输出不同?你知道吗
class A:
def m(self):
print("m of A called")
class B(A):
pass
class C(A):
def m(self):
print("m of C called")
class D(B,C):
pass
x = D()
x.m()
实际输出:
$ python diamond1.py //python 2 used for the code
m of A called
$ python3 diamond1.py //python 3 used for the code
m of C called
有人能告诉我们方法(方法m)是如何被调用的(调用的顺序),为什么以及它们在python2和python3中的实现有什么不同吗?你知道吗
这种区别是python2特有的,并且与旧样式和新样式的类有关(python3只有后者)。特别是,这两种类型的类使用不同的方法解析顺序。你知道吗
有关详细信息,请参见https://wiki.python.org/moin/NewClassVsClassicClass
如果您这样更改代码:
你将得到一致的行为,因为这将使一切成为一个新的风格类。你知道吗
旧式类的存在只是为了与python2.1和更早版本兼容(我们说的是2001年)。你知道吗
相关问题 更多 >
编程相关推荐