描述:
1. __init__ 实例初始化
2. __new__ 创建实例
3. __call__ 实例调用
4. __del__ 实例删除(显式调用, 最后一个实例引用时会删除; 非显式调用, 程序结束时会自动调用)
代码:
# -*- coding: utf8 -*-
class First(object):
def __init__(self):
print("First init...")
def __new__(cls, *args, **kwargs):
print("Frist new...")
return super(First, cls).__new__(cls)
def __call__(self, *args, **kwargs):
print("First call...")
def __del__(self):
print("First del...")
class Second(First):
def __int__(self):
print("Second init...")
def __new__(cls, *args, **kwargs):
print("Second init...")
a = super(Second, cls).__new__(First)
print(a)
a.__init__()
return a
if __name__ == '__main__':
s = Second()
print("s:",s)
print("* * " * 30)
print(s())
s1 = s
print("del s1")
del s1
print("un del s")
# del s
输出:
Second init...
Frist new...
<__main__.First object at 0x000002146A0E8CF8>
First init...
s: <__main__.First object at 0x000002146A0E8CF8>
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
First call...
None
del s1
un del s
First del...