Pthon魔法方法
在python中,有一些内置好的特定的方法,这些方法在进行特定的操作时会自动被调用,称之为魔法方法
基础魔法方法(较为常用)
init
构造方法,在初始化类时使用
new
在构造对象是第一个被调用,是真正意义上的构造函数
将参数传给__init__函数
一般不会重写__new__函数
del
析构函数
str
定义当被 str() 调用时的行为
属性相关的方法
定义当描述符的值被取得时的行为:get(self, instance, owner)
定义当描述符的值被改变时的行为:set(self, instance, value)
定义当描述符的值被删除时的行为:delete(self, instance)
各类运算符
例如:
le(self, other)
eq(self, other)
gt(self, other)
内容来源
https://www.jianshu.com/p/3f4786b33f34
https://blog.youkuaiyun.com/qq_38520096/article/details/79237593
阿里云天池Python训练营