Bitwise operations in class inheriting int

前端 未结 2 1154
时光取名叫无心
时光取名叫无心 2021-01-20 05:55

I\'ve inherited from int, because I wanted to implement a simple interface for bitwise operations. Due to the non mutability of int, I have to use the integer member functio

2条回答
  •  半阙折子戏
    2021-01-20 06:09

    There is already simple interfaces for bitwise operations (see http://docs.python.org/reference/expressions.html#summary) e.g :

    4 >> 1
    # 2
    

    You don't have __ior__ and __iand__ but you do have __and__ and __or__. But I suspect that what you want is actually __ror__ and __rand__ which implement bitwise operations. Integers do have those last magic methods. On magic methods see : http://www.rafekettler.com/magicmethods.html#numeric

    See also : http://wiki.python.org/moin/BitwiseOperators

提交回复
热议问题