python学习之__doc__,__module__,__class__,__del__,__call__,__iter__,__next__

__doc__
class Foo:
'我是描述信息'
pass

class Bar(Foo):
pass
# print(Bar.__doc__) #该属性无法继承给子类

# print(Bar.__doc__) #该属性无法继承给子类


__module__ __class__
from lib.aa import C


c1=C()
print(c1.name)

print(c1.__module__)
print(c1.__class__)

__del__
class Foo:
def __init__(self,name):
self.name=name
def __del__(self):
print('我执行啦')

f1=Foo('alex')

# del f1 #删除实例会触发__del__
del f1.name #删除实例的属性不会触发__del__
print('--------------------->')

#程序运行完毕会自动回收内存,触发__del__

__call__

class Foo:
def __call__(self, *args, **kwargs):
print('实例执行啦 obj()')

f1=Foo()

f1() #f1的类Foo 下的__call__

Foo() #Foo的类 xxx下的__call__

 

__iter__,__next__

class Foo:
def __init__(self,n):
self.n=n
def __iter__(self):
return self

def __next__(self):
if self.n == 13:
raise StopIteration('终止了')
self.n+=1
return self.n

# l=list('hello')
# for i in l:
# print(i)
f1=Foo(10)
# print(f1.__next__())
# print(f1.__next__())
# print(f1.__next__())
# print(f1.__next__())

for i in f1: # obj=iter(f1)------------>f1.__iter__()
print(i) #obj.__next_()

posted on 2020-04-25 11:17  秋雨的蝴蝶  阅读(117)  评论(0编辑  收藏  举报