old style class和newstyle class
>>> class A:pass
>>> type(A)
<type ‘classobj’>
>>> type(int)
<type ‘type’>
>>> class A1(object):pass
>>> type(A1)
<type ‘type’>
18.
new style class的作用
• 在Python 2.2之前,类与类型是不统一的
• 在Python 2.2实现了统一
• 让内置的类型子类化更方便
• 提供了属性(property)的机制
• 实现了static和class方法
• 提供了metaclass编程
Unifying types and classes in Python 2.2
PEP-252: Making types look more like classes
PEP-253: Subtyping built-in types
19.
类
class A(object):pass
class B(A):
“””B description”””
def out_b(self, name):
print name
class C(A):
def out_c(self, name):
print name
class D(B, C):pass
>>> d = D()
>>> d.out_b('hello')
hello