What is Practical Use of Reversed Set Operators in Python?


Reversed set operators are operators that are defined as:

s & z corresponds to s.__and__(z)
z & s corresponds to s.__rand__(z)

These don't make much sense in normal operations like and, add, or, etc of simple objects. However in case of inheritence, reversed operations are particularly useful when dealing with subclasses because if the right operand is a subclass of the left operand the reversed operation is attempted first. You may have different implementations in parent and child classes.

These reversed operations are also used if the first operand returns NotImplemented.

Updated on: 30-Jul-2019

89 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements