天池python学习-task03打卡第九天

反算术运算符

反运算魔方方法,与算术运算符保持一一对应,不同之处就是反运算的魔法方法多了一个“r”。当文件左操作不支持相应的操作时被调用。

  • __radd__(self, other)定义加法的行为:+
  • __rsub__(self, other)定义减法的行为:-
  • __rmul__(self, other)定义乘法的行为:*
  • __rtruediv__(self, other)定义真除法的行为:/
  • __rfloordiv__(self, other)定义整数除法的行为://
  • __rmod__(self, other) 定义取模算法的行为:%
  • __rdivmod__(self, other)定义当被 divmod() 调用时的行为
  • __rpow__(self, other[, module])定义当被 power() 调用或 ** 运算时的行为
  • __rlshift__(self, other)定义按位左移位的行为:<<
  • __rrshift__(self, other)定义按位右移位的行为:>>
  • __rand__(self, other)定义按位与操作的行为:&
  • __rxor__(self, other)定义按位异或操作的行为:^
  • __ror__(self, other)定义按位或操作的行为:|

a + b

这里加数是a,被加数是b,因此是a主动,反运算就是如果a对象的__add__()方法没有实现或者不支持相应的操作,那么 Python 就会调用b__radd__()方法。

增量赋值运算符

  • __iadd__(self, other)定义赋值加法的行为:+=
  • __isub__(self, other)定义赋值减法的行为:-=
  • __imul__(self, other)定义赋值乘法的行为:*=
  • __itruediv__(self, other)定义赋值真除法的行为:/=
  • __ifloordiv__(self, other)定义赋值整数除法的行为://=
  • __imod__(self, other)定义赋值取模算法的行为:%=
  • __ipow__(self, other[, modulo])定义赋值幂运算的行为:**=
  • __ilshift__(self, other)定义赋值按位左移位的行为:<<=
  • __irshift__(self, other)定义赋值按位右移位的行为:>>=
  • __iand__(self, other)定义赋值按位与操作的行为:&=
  • __ixor__(self, other)定义赋值按位异或操作的行为:^=
  • __ior__(self, other)定义赋值按位或操作的行为:|=

一元运算符

  • __neg__(self)定义正号的行为:+x
  • __pos__(self)定义负号的行为:-x
  • __abs__(self)定义当被abs()调用时的行为
  • __invert__(self)定义按位求反的行为:~x

属性访问

  • __getattr__(self, name): 定义当用户试图获取一个不存在的属性时的行为。
  • __getattribute__(self, name):定义当该类的属性被访问时的行为(先调用该方法,查看是否存在该属性,若不存在,接着去调用__getattr__)。
  • __setattr__(self, name, value):定义当一个属性被设置时的行为。
  • __delattr__(self, name):定义当一个属性被删除时的行为。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值