Class definition parameter vs. Normal class definition
本问题已经有最佳答案,请猛点这里访问。
这两者有什么区别?这两者的功能不相同吗?我不明白
1 2 3 4 | class Car(object): # object parameter def foobar(): print("Hello World! ") |
VS
1 2 3 4 | class Car(): # No parameter def foobar(): print("Hello World! ") |
在python 2中,前者是一个"新样式类",后者是一个"旧样式类",只存在于向后兼容性中。你不应该将后者用于任何新事物。
在python 3中,我认为根本没有区别。你甚至可以完全不加括号。
两个字:
1 2 3 | # new style class Car(object): pass |
A"New Class" is the recommended way to create a class in modern
Python.
1 2 3 | # classic style class Car(): pass |
A"Classic Class" or"old-style class" is a class as it existed in
Python 2.1 and before. They have been retained for backwards
compatibility. This page attempts to list the differences.
请看:
- http://python-history.blogspot.com/2010/06/new-style-classes.html
- https://wiki.python.org/moin/newclassvsclassicclass
- https://www.python.org/doc/newstyle/