2019年9月2日 __module__ and __ class__ and __del__ and __call__

from test1 import C

c1=C()
print(c1.name)

print(c1.__module__)#查询来自于哪个模块
print(c1.__class__)#查询来自于哪个类

>>>

C
test1
<class 'test1.C'>

__del__ 析构方法:当对象在内存被释放时,自动触发执行。

class Foo:
    def __init__(self):
        self.name='sxj'

    def __del__(self):#在垃圾回收时才会触发,只是执行del 不会触发
        print('I am __del__')

f1=Foo()
# del f1.name
print("_____>>>")

》》》

_____>>>
I am __del__

__call__  对象后面加括号,触发执行。

class Foo:
    def __init__(self):
        self.name='sxj'

    def __call__(self, *args, **kwargs):
        print('实例执行call')


f1=Foo()
f1() #调用 Foo 下面的call 方法

》》》》》

实例执行call

原文地址:https://www.cnblogs.com/python1988/p/11449052.html