类声明之间的区别 [英] Difference between class declarations

查看:85
本文介绍了类声明之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些类似的问题,但我想确定,所以我问...

I see some similar questions about this topic, but i wish to be sure, so i am asking...

有什么区别:

class MyClass:
    pass

class MyClass():
    pass

此外,这两者之间是否有区别:

Also, is there a difference between these two:

class MyClass():
    pass

class MyClass(object):
    pass


推荐答案

前两个拼写没有区别。

python 2.7,在后两个之间有一个巨大的区别。继承对象使其成为 new-style类,改变继承语义并添加对描述符的支持( @property @classmethod ,等等)。这是Python 3中的默认值。

In python 2.7, there is a huge difference between the latter two. Inheriting from object makes it a new-style class, changing the inheritance semantics and adding support for descriptors (@property, @classmethod, etc.). It's the default in Python 3.

在Python 2.2中引入了新式类来统一类型(例如 int list )和类,并且因为一些事情以向后不兼容的方式发生变化,所以需要'opt in',显式继承 object

New-style classes were introduced in Python 2.2 to unify types (such as int and list), and classes, and because several things change in backwards-incompatible ways, you need to 'opt in', explicitly inherit from object to enable the changes.

在Python 3中,继承自 object 需要,课程是新式的,总是。

In Python 3, inheriting from object is no longer needed, classes are new-style, always.

这篇关于类声明之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆