I noticed all 3 -> class foo
, class foo()
and class foo(object)
can be used but i am confused as to what is the difference between these 3, if there is any? (I mean in properties mainly, python3
)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Let's break them down:
class foo
:- Python 3: It's usually the way to go. By default, Python adds
object
as the base class for you. - Python 2: It creates an old style
classobj
that will cause you all sorts of headaches.
- Python 3: It's usually the way to go. By default, Python adds
class foo()
:- Python 3 and Python 2: Similar to
class foo
for both Python versions, trim it off, it looks ugly and makes no difference.
- Python 3 and Python 2: Similar to
class foo(object)
:- Python 3 and Python 2: In both Pythons, results in a new style class that has all the goodies most know. People usually use this form when writing code that might be used in Python
2
too, explicitly inheriting from object causes the class to be new style in Python 2 and makes no difference in 3 (apart from some extra typing).
- Python 3 and Python 2: In both Pythons, results in a new style class that has all the goodies most know. People usually use this form when writing code that might be used in Python