在 python 2.x 中有一些以新样式定义类的动议
class Foo(object):
比较老式
class Foo:
对于 python 3.x,我读到这种歧义消失了。但我不确定这意味着什么。
两个问题:
- python 3.x 中类定义的正确样式是什么
- 对最终将升级到 python 3.x 代码的 python 2.x 代码的类定义有何建议
最佳答案
在 Python 3 中,所有类都是“新式”的。这两种语法在 Python 3.x 中做同样的事情。
What is the proper style for class definitions in python 3.x
class Foo:
What is the recommendation for class definitions for python 2.x code
在 Python 2.x 中,建议在编写新代码时始终使用新式类。无论您是否计划升级到 Python 3.x,这都适用。
class Foo(object):
关于python - 2.x 中 python 类样式的建议是什么,最终更新到 3.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13304405/