Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 两个类定义之间的区别是什么_Python_Class_Inheritance - Fatal编程技术网

Python 两个类定义之间的区别是什么

Python 两个类定义之间的区别是什么,python,class,inheritance,Python,Class,Inheritance,在Python 2中,类应该显式地定义为对象的子类。在Python3中,这将是 默认 >>> class A(object): pass >>> class B(): pass >>> type(B) <type 'classobj'> >>> type(A) <type 'type'> >>A类(对象): 通过 >>>B类(): 通过 >>>类型(B) >>>类型(A) 我使

在Python 2中,类应该显式地定义为对象的子类。在Python3中,这将是 默认

>>> class A(object):
    pass

>>> class B():
    pass

>>> type(B)
<type 'classobj'>
>>> type(A)
<type 'type'>
>>A类(对象):
通过
>>>B类():
通过
>>>类型(B)
>>>类型(A)
我使用Python2.7,正如我在2.7中所知道的那样,类继承自Python2.2中引入的所谓“新样式对象”

新样式对象与经典对象具有不同的对象模型,有些东西无法与旧样式对象正常工作,例如super()、@property和描述符

在著名的问题中有更多关于它的内容:

另请参阅:


另外,请注意,它们之间只有在Python2中才有区别。在Python3中,这两种类型的声明已经没有区别了(我知道你的问题是关于Python2的,只是一个小提示)。

阅读本文了解新样式类的解释?我非常确定
class
继承自Python>=3.0中的
object
,而不是2.7。您仍然需要显式继承python 2.x中的
对象
。哪一个是新样式的对象?@omeinusch:first一个,
A