面向对象——内置函数

__str__函数 和__del__函数

__str__: 会在对象被打印时自动触发,然后将返回值返回给print功能进行打印

class Foo:
    def __str__(self):
        return "1111"

f1 = Foo()
print(f1)
#1111

__del__用于类删除之前的清理工作

class Bar:
    def __init__(self,x,y,filepath):
        self.x=x
        self.y=y
        self.f=open(filepath,'r',encoding='utf-8')
    def __del__(self):
        # 写回收系统资源相关的代码
        self.f.close()

obj=Bar(10,20)
del obj
原文地址:https://www.cnblogs.com/msj513/p/9872998.html