Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python __ixor__() Magic Method

#1
Python __ixor__() Magic Method

Syntax


object.__ixor__(self, other)

The Python __ixor__() magic method implements the in-place bitwise XOR x ^= y that calculates the result of the bitwise XOR operation x ^ y, and assigns it to the first operands’ variable x. This type of in-place operation is also called augmented arithmetic assignment. The method simply returns the new value to be assigned to the first operand.

  • When you call x ^= y, Python first attempts to call x.__ixor__(y).
  • If this is not implemented, it tries the normal bitwise AND operation x.__xor__(y).
  • If this is not implemented either, it tries reverse exponentiation operation y.__rxor__(x) with swapped operands.

The result is then assigned to the first operand x. If none of those operations is implemented, Python raises a TypeError.

We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

Basic Example Overriding __ixor__


In the following code example, you create a class Data and define the magic method __ixor__(self, other).

  • The “self” argument is the default argument of each method and it refers to the object on which it is called—in our case, the first operand of the in-place operation.
  • The “other” argument of the in-place method refers to the second operand, i.e., y in the in-place operation x ^= y.

The return value of the operation returns a dummy string 'finxter 42' to be assigned to the first operand. In practice, this would be the result of the in-place bitwise XOR operation.

class Data: def __ixor__(self, other): return 'finxter 42' x = Data()
y = Data() x ^= y print(x)
# finxter 42

In-Place XOR ^= without __ixor__()


To support the in-place bitwise XOR operation on a custom class, you don’t have to overwrite the __ixor__() method. Because if the method is not defined, Python will fall back to the normal __xor__() method and assign its result to the first operand.

Here’s an example:

class Data: def __xor__(self, other): return 'finxter 42' x = Data()
y = Data() x ^= y print(x)
# finxter 42

Even though the __ixor__() method is not defined, the in-place bitwise XOR operation x ^= y still works due to the __xor__() “fallback” magic method!

In-Place XOR ^= without __ixor__() and __xor__()


To support in-place bitwise XOR x ^= y on a custom class, you don’t even have to overwrite any of the x.__ixor__(y) or x.__xor__(y) methods. If both are not defined, Python falls back to the reverse y.__rxor__(x) method and assigns its result to the first operand.

Here’s an example where you create a custom class for the first operand that doesn’t support the bitwise XOR operation. Then you define a custom class for the second operand that defines the __rxor__() method. For the in-place operation, Python falls back to the __rxor__() method defined on the second operand and assigns it to the first operand x:

class Data_1: pass class Data_2: def __rxor__(self, other): return 'finxter 42' x = Data_1()
y = Data_2() x ^= y print(x)
# finxter 42

TypeError: unsupported operand type(s) for ^=


If you try to perform in-place bitwise OR x ^= y but neither x.__ixor__(y), nor x.__xor__(y), nor y.__rxor(x) is defined, Python raises a “TypeError: unsupported operand type(s) for ^=". To fix this error, simply define any of those methods before performing the in-place operation.

class Data: pass # ... you should define __ixor__ here to prevent error! ... # x = Data()
y = Data() x ^= y

Output:

Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 8, in <module> x ^= y
TypeError: unsupported operand type(s) for ^=: 'Data' and 'Data'

Background Bitwise XOR




Python’s bitwise XOR operator x ^ y performs logical XOR on each bit position on the binary representations of integers x and y. Each output bit evaluates to 1 if and only if exactly one of the two input bits at the same position are 1.

For example, the integer expression 4 ^ 3 is translated to the binary operation 0100 ^ 0011 which results in 0111 because for the last three positions exactly one bit is 1.

You can learn more in our in-depth tutorial on this operator:

Related Video Compound Operators




References:

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python __ixor__() Magic Method first appeared on Finxter.



https://www.sickgaming.net/blog/2021/11/...ic-method/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
Steam - Team Fortress 2 Update Released
Last Post: GilbertBib
Today 08:34 AM
» Replies: 1
» Views: 128
[Tut] Pandas cut() – A Simple Guide with...
Last Post: xSicKxBot
Yesterday 10:59 PM
» Replies: 0
» Views: 30
(Indie Deal) Grab Die Young for FREE & c...
Last Post: xSicKxBot
Yesterday 10:59 PM
» Replies: 0
» Views: 4
Blender 3 Released
Last Post: xSicKxBot
Yesterday 10:59 PM
» Replies: 0
» Views: 4
AppleInsider - Apple execs explain the d...
Last Post: xSicKxBot
Yesterday 10:58 PM
» Replies: 0
» Views: 3
Microsoft - ID@Xbox Winter Game Fest Dem...
Last Post: xSicKxBot
Yesterday 10:58 PM
» Replies: 0
» Views: 4
News - Talking Point: What Can This $120...
Last Post: xSicKxBot
Yesterday 10:58 PM
» Replies: 0
» Views: 5
Xbox Wire - Get Ready for the Alien Inva...
Last Post: xSicKxBot
Yesterday 10:58 PM
» Replies: 0
» Views: 5
News - Tsareena brings grass-fuelled glo...
Last Post: xSicKxBot
Yesterday 10:58 PM
» Replies: 0
» Views: 4
[Tut] User Registration in PHP with Logi...
Last Post: GilbertBib
Yesterday 01:45 PM
» Replies: 1
» Views: 53

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com



Discord Server © SickGaming.net 2012-2021