手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

如何在PYTHON中的class里运用__del__内置方法

时间:2024-10-15 16:49:03

1、class Flower: def __init__(self, name): self.name = name print("This is %s" %self.name) dandelion = Flower("dandelion")#首先我们定义一个类,这是一个花类,属性是打印花的品名。

如何在PYTHON中的class里运用__del__内置方法

2、class Flower: def __init__(self, name): self.name = name print("This is %s" %self.name) def __del__(): #这里要运用__del,定义的方法类似函数。

如何在PYTHON中的class里运用__del__内置方法

3、class Flower: def __init__(self, name): self.name = name print("This is %s" %self.name) def __del__(self): #但是务必记得要加上self。

如何在PYTHON中的class里运用__del__内置方法

4、clas衡痕贤伎s Flower: def __init__(self, name): self.name = name print("This is %s" %self.name) def __del__(self): print("The end!") dandelion = Flower("dandelion") #设置后再运行一下就会发现最后多了一行,其实就是程序运行的最后会显示__del__的设置。

如何在PYTHON中的class里运用__del__内置方法

5、class Flower: def __init__(self, name): self.name = name print("This is %s" %self.name) def __del__(self): print("The end of %s" %self.name) dandelion = Flower("dandelion")#同样的,__del__也是可以调用属性的。

如何在PYTHON中的class里运用__del__内置方法

6、clas衡痕贤伎s Flower: def __init__(self, name): self.name = name print("This is %s" %self.name) def __del__(self): print("The end of %s" %self.name)del dandeliondandelion = Flower("dandelion")#从这个语句就可以看出执行的顺序了。

如何在PYTHON中的class里运用__del__内置方法
© 手抄报圈