本文全部實例代碼在python3.7下python
class Foo: def __new__(cls, *args, **kwargs): return super().__new__(cls) def __init__(self, x, y): self.__x = x self.__y = y foo = Foo(10, 20)
__init__返回爲None;
__new__返回了一個建立的實例,其後做爲__init__中的self傳入app
super().__new__(cls)
# cls爲當前要建立的類對象
看一個返回類實例以外的例子:blog
class Foo: def __init__(self, x): self.__x = x @property def x(self): return self.__x class Bar: def __new__(cls, *args, **kwargs): foo = super().__new__(Foo) foo.__init__(*args, **kwargs) return foo bar = Bar(10) print(bar.x)
看一個返回自身類實例的:rem
class Bar: def __new__(cls, *args, **kwargs): foo = super().__new__(cls) return foo def __init__(self, x): self.__x = x @property def x(self): return self.__x bar = Bar(10) print(bar.x)
大多數狀況下都不須要重寫__new__。it
class Foo: __instance = None def __new__(cls, *args, **kwargs): if not cls.__instance: cls.__instance = super().__new__(cls) return cls.__instance foo1 = Foo() foo2 = Foo() print(foo1, foo2)
輸出:
<__main__.Foo object at 0x0000029A4B879048> <__main__.Foo object at 0x0000029A4B879048>
能夠看出foo1和foo2是同一個實例class
class Foo: __instance = [] limit = 2 def __new__(cls, *args, **kwargs): print(len(cls.__instance)) if len(cls.__instance) == cls.limit: raise RuntimeError("Count not create instance. Limit %s reached" % cls.limit) instance = super().__new__(cls) cls.__instance.append(instance) return instance def __del__(self): self.__instance.remove(self) foo1 = Foo() foo2 = Foo() print(foo1, foo2)
您能夠自定義建立的實例,並在調用初始化程序__init__以前對其進行一些操做。此外,您能夠基於某些約束對實例建立施加限制object
def is_create(): #根據條件判斷是否能夠建立 return True class Foo: def __new__(cls, a, b): if not is_create(): raise RuntimeError('實例不能被建立') instance = super().__new__(cls) instance.count = a + b return instance def __init__(self, a, b): pass foo = Foo(1, 2) print(foo.count)
一般,當您實例化類時,它將返回該類的實例。您能夠自定義此行爲,而且能夠返回所需的對象。程序
class Foo: def __new__(cls, a, b): instance = super().__new__(cls) instance.__init__(a, b) return a + b def __init__(self, a, b): print('a+b') foo = Foo(1, 2) print(foo)
若是咱們不從__new__方法返回實例對象,則必須顯式調用__init__。方法