python 魔法函数

         Python 中的魔法函数(Magic Methods)是一种特殊的方法,它们以双下划线开头和结尾(例如 __init__)。这些方法允许你定制类的行为,使其具有类似于内置类型的一些特性,比如迭代、比较、运算符重载等。以双下划线开头和结尾的函数称为魔法函数:

__init__(self,...): 用于初始化self。

__str__ (self):将实例返回实例的字符串表示,自定义内容的字符串。

__len__ (self):使用len函数,返回实例对应的长度,此返回值可自定义。

__gt__ (self, other):大于,使用 > 触发。

__ge__ (self, other):大于等于,使用>=触发。

__lt__ (self, other):小于,使用<触发。

__le__ (self, other):小于等于,使用<=触发。

__eq__ (self, other):等于,使用==触发。

__ne__ (self, other):不等于,使用!= 触发。

__and__ ():按位与。

__add__ (self, other):加,使用+触发。

__sub__ (self, other):减,使用-触发。

__mul__ (self, other): 乘,使用 * 触发。

__truediv__ (self, other): 真除(除),使用/触发。

__floordiv__ (self, other): 整除,使用//触发。

__divmod__ (self, other): 先除后余,使用div(x,y)触发。

__mod__ (self, other): 求余,使用%触发。

__contains__(self, item)定义 in 操作符的行为,用于判断对象是否包含某个元素

__getitem__(self, key): 定义索引操作,支持 obj[key] 的使用。

__setitem__(self, key, value): 定义赋值操作,支持 obj[key] = value 的使用。

__delitem__(self, key): 定义删除操作,支持 del obj[key] 的使用。

__iter__(self): 定义迭代操作,返回一个迭代器对象,支持对象的迭代。

__del__(self): 析构函数,用于释放对象,在对象被销毁前调用。

__repr__(self): 定义对象的“官方”字符串表示,通常用于调试和日志记录。

__next__(self): 定义迭代器的下一个元素,配合 __iter__ 使用。

__getattr__(self, name): 定义属性访问行为,当访问不存在的属性时调用。

__setattr__(self, name, value): 定义属性赋值行为,当设置属性时调用。

__call__(self, ...): 定义对象调用行为,使对象可以像函数一样调用。

        魔法函数提供了丰富的功能,使得自定义类的行为更加灵活和强大,让对象可以表现得更像内置类型一样。

class YS:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return self.name

    def __len__(self):
        return len(self.name)

    def __gt__(self, other):
        return self.age > other.age

    def __ge__(self, other):
        return self.age >= other.age

    def __lt__(self, other):
        return self.age < other.age

    def __le__(self, other):
        return self.age <= other.age

    def __eq__(self, other):
        return self.age == other.age

    def __ne__(self, other):
        return self.age != other.age

    def __add__(self, other):
        return self.age + other.age

    def __sub__(self, other):
        return self.age - other.age

    def __mul__(self, other):
        return self.age * other.age

    def __truediv__(self, other):
        return self.age / other.age

    def __floordiv__(self, other):
        return self.age // other.age

    def __divmod__(self, other):
        return divmod(self.age, other.age)

    def __mod__(self, other):
        return self.age % other.age


ys0 = YS("x", 2000)
ys1 = YS("kk", 10000)
ys2 = YS("ln", 16)
ys3 = YS("tnl", 23)
ys4 = YS("sn", 23)
print(len(ys0), len(ys1))
print(ys0 > ys1, ys2 >= ys3)
print(ys0 < ys1, ys2 <= ys3)
print(ys3 == ys4, ys0 != ys1)
print(ys2 + ys3, ys3 - ys2, ys3 * ys4, ys1 / ys0)
print(ys1 // ys0, ys1 % ys0)
print(divmod(ys1, ys0))

 

  • 15
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值