通常我们按照Java或C++里的习惯, 称__init__
方法为构造器方法, 其实这种说法并不准确, 因为类的实例在调用__init__
方法之前已经在__new__
方法里创建了. 所以说, 真正的构造器方法应该是__new__
, 而__init__
只是完成构造以外的初始化操作. 用Fluent Python里的伪代码描述实例的构造过程:
具体描述为: 先调用the_class.__new__(some_arg)
, 如果它的返回值new_object
是the_class
类型的实例, 则继续调用the_class.__init__(new_object, some_arg)
, 否则直接返回.
值得注意的是__new__(cls, ...)
是一个classmethod
, 但它的定义并不需要用@classmethod
, 因为它会被编译器特殊照顾.
大部分情况下我们只需要实现__init__
方法而不需要实现__new__
方法. 我暂时能想到的实现__new__
方法的唯一用途是实现一个类似于Python内建类property
的类, 它即是类, 也是一个修饰器:
class Foo():
@property
def name(self):
return 'Foo'
def get_age(self):
return 10
age = property(get_age)
print(Foo.name, Foo.age)
foo = Foo()
print(foo.name, foo.age)
输出:
<property object at 0x7fd135e604f8> <property object at 0x7fd135ef51d8>
Foo 10
借助__new__
方法, 我们也可以实现类似的效果, 让一个类既当类又当修饰器, 当然, 这不是唯一的方法:
class cls_decorator(object):
def __init__(self):
print('__init__ is called')
def __new__(cls, fn = None):
if fn is not None and callable(fn):
def new_fn(*args, **kwargs):
print('decorated', fn.__name__, 'is called.')
result = fn(*args, **kwargs)
return result
return new_fn
else:
return super().__new__(cls)
@cls_decorator
def fn(a, b, c):
return 3
print('As a decorator:', fn(1, 2, c = 3))
print('As a class:', cls_decorator())
输出:
decorated fn is called.
As a decorator: 3
__init__ is called
As a class: <__main__.cls_decorator object at 0x7fd135e7b828>