这是一个老样式类: class OldStyle:
pass
这是一种新式类: class NewStyle(object):
pass
这也是一个新式类: class NewStyle2:
__metaclass__ = type
有NewStyle和NewStyle2之间有什么不同? 我的印象是,从object继承的唯一影响实际上是定义了type元类,
考虑经典类和新样式类之间的以下差异。 class A():
data = 'abcd'
def __getattr__(self, name):
return getattr(self.data, name)
class B(object):
data = 'abcd'
def __getattr__(self, name)
可能重复: Old style and new style classes in Python 什么是新的风格和老式类事务在Python 2.7的现状如何?我经常不使用Python,但我依稀记得这个问题。文档似乎没有提到这个问题:The Python Tutorial: Classes。我仍然需要担心这个吗?在一般情况下,我应该宣布我的类如下: class MyClass:
pass