Implementing other special methods

In addition to the core arithmetic and comparison operators, we have a group of additional operators that (generally) we only define for the numbers.Integral values. As we're not defining integral values, we can avoid these special methods:

Method

Operator

object.__lshift__(self, other)

<<

object.__rshift__(self, other)

>>

object.__and__(self, other)

&

object.__xor__(self, other)

^

object.__or__(self, other)

|

 

Also, there are reflected versions of these operators:

Method

Operator

object.__rlshift__(self, other)

<<

object.__rrshift__(self, other)

>>

object.__rand__(self, other)

&

object.__rxor__(self, other)

^

object.__ror__(self, other)

|

 

Additionally, there is a unary operator for a bit-wise inverse of the value:

Method

Operator

object.__invert__(self)

~

 

Interestingly, some of these operators are defined for the set collection, as well as integral numbers. They don't apply to our rational value. The principles to define these operators are the same as the other arithmetic operators.

Now, let's see how to optimize using the in-place operators.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset