__call__ 与__init__,object 参数的使用

class test1:  ###有object是可读可写
def __init__(self): ##__init__表示构造函数。__call__是析构函数。
self.__pravite = "alex 1"
##@property
def show(self):
return (self.__pravite)

class test2(object): ##没有object后是只读,需要写的话要加装饰器。
def __init__(self):
self.__pravite = "alex 2"
@property
def show(self):
return self.__pravite
@show.setter ##定义装饰器
def show(self,value):
self.__pravite = value

t1 = test1() ##表示执行__init__对象
print (t1.show)
t1.show = "change 1"
print (t1.show)

t2 = test2()
print (t2.show)
t2.show = "change 2"
print (t2.show)

print ("______以下是析构函数______")

class Foo:
def __init__(self):
print ("现在执行__init__")
pass
def __del__(self):
print ("最后的呐喊")
def Go(self):
print ("Go!!!")
def __call__(self):
print ("call方法")

f1 = Foo() ##执行 Foo里的__init__方法
f1.Go()
f1() ##执行类的__call__方法,然后执行__del__
原文地址:https://www.cnblogs.com/liulvzhong/p/7609916.html