Learning objectives
By the end of this section you should be able to
- Identify magic methods and describe their purpose.
- Develop overloaded arithmetic and comparison operators for user-defined classes.
Magic methods and customizing
Magic methods are special methods that perform actions for users, typically out of view of users. Magic methods are also called dunder methods, since the methods must start and end with double underscores (__). Ex: __init__()
is a magic method used alongside __new__()
to create a new instance and initialize attributes with a simple line like eng = Engineer()
. A programmer can explicitly define a magic method in a user-defined class to customize the method's behavior.
Concepts in Practice
Magic methods
Overloading arithmetic operators
Operator overloading refers to customizing the function of a built-in operator. Arithmetic operators are commonly overloaded to allow for easy changes to instances of user-defined classes.
Arithmetic operator (Operation) | Magic method |
---|---|
+ (Addition) | __add__(self, other) |
- (Subtraction) | __sub__(self, other) |
* (Multiplication) | __mul__(self, other) |
/ (Division) | __truediv__(self, other) |
% (Modulo) | __mod__(self, other) |
** (Power) | __pow__(self, other) |
Concepts in Practice
Arithmetic operator overloading
Overloading comparison operators
Comparison operators can also be overloaded like arithmetic operators.
Comparison operator (Operation) | Magic method |
---|---|
< (Less than) | __lt__(self, other) |
> (Greater than) | __gt__(self, other) |
<= (Less than or equal to) | __le__(self, other) |
>= (Greater than or equal to) | __ge__(self, other) |
== (Equal) | __eq__(self, other) |
!= (Not equal) | __ne__(self, other) |
Example 11.2
Overloading comparison operators for the Account class
Code | Output |
---|---|
class Account:
def __init__(self, name="", amount=0):
self.name = name
self.amount = amount
def __str__(self):
return f"{self.name}: ${self.amount}"
def __lt__(self, other):
return self.amount < other.amount
def __gt__(self, other):
return self.amount > other.amount
def __eq__(self, other):
return self.amount == other.amount
acct_a = Account("Ashe", 6492)
acct_b = Account("Bevins", 5210)
print(acct_a < acct_b)
print(acct_a > acct_b)
acct_a.amount = 5210
print(acct_a == acct_b)
|
False True True
|
Concepts in Practice
Comparison operator overloading
Consider the example above.
Try It
Combining exercise logs
The ExerciseLog
class has two instance attributes: e_type
, the type of exercise, and duration
, the time spent exercising.
Overload the + operator to combine two ExerciseLogs
such that:
- If the exercise types are different, combine them with
" and "
in between. Else, use the same type and don't duplicate. - Add durations together.
Given input:
walk 5 run 30
The output is:
walk and run: 35 minutes
Try It
Expanding the Account class
Using isinstance()
allows the programmer to define different behaviors depending on an object's type. The first parameter is the object, and the second parameter is the type or class. Ex: isinstance(my_var, int)
returns True
if my_var
is an integer.
Expand the existing Account
example so that the addition operator can also be used to add an integer value to an Account'
s amount attribute.