Python类定义语法 [英] Python class definition syntax

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

问题描述

class A:
    ...

class A():
    ...



我刚刚意识到我的几个类被定义为前者和他们工作很好。

I just realized that a couple of my classes are defined as the former and they work just fine. Do the empty parenthesis make any difference?

推荐答案

后者是旧版本的Python的语法错误。在Python 2.x中,你应该尽可能从 object 派生,因为一些有用的功能只能用于新式类(派生自 object 在Python 3.x中是可选的,默认值为)。

The latter is a syntax error on older versions of Python. In Python 2.x you should derive from object whenever possible though, since several useful features are only available with new-style classes (deriving from object is optional in Python 3.x, since new-style classes are the default there).

这篇关于Python类定义语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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