Python-b04-Special(Magic/Dunder) Methods

This article shows the examples of special(magic/dunder) methods in python

basic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Employee:
raise_amount = 2.04

def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'

def fullname(self):
return '{} {}'.format(self.first, self.last)

def apply_raise(self):
self.pay = int(self.pay * self.raise_amount)

def set_regular_raise_amount(self, amount):
self.raise_amount = amount

@classmethod
def set_class_raise_amount(cls, amount):
cls.raise_amount = amount

@staticmethod
def is_workday(day):
if day.weekday() == 5 or day.weekday() == 6:
return False
return True

emp_1 = Employee('Bob', 'Gobdo', 20)
print(emp_1) # <__main__.Employee object at 0x00000200249F63A0>

__repr__ method is used for debug

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    class Employee:
raise_amount = 2.04

def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'

def fullname(self):
return '{} {}'.format(self.first, self.last)

def apply_raise(self):
self.pay = int(self.pay * self.raise_amount)

def set_regular_raise_amount(self, amount):
self.raise_amount = amount

@classmethod
def set_class_raise_amount(cls, amount):
cls.raise_amount = amount

@staticmethod
def is_workday(day):
if day.weekday() == 5 or day.weekday() == 6:
return False
return True

def __repr__(self):
return "Employee('{}','{}',{})".format(self.first, self.last, self.pay)

emp_1 = Employee('Bob', 'Gobdo', 20)
print(emp_1) #Employee('Bob','Gobdo',20)

__str__ method is used for user

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Employee:
raise_amount = 2.04

def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'

def fullname(self):
return '{} {}'.format(self.first, self.last)

def apply_raise(self):
self.pay = int(self.pay * self.raise_amount)

def set_regular_raise_amount(self, amount):
self.raise_amount = amount

@classmethod
def set_class_raise_amount(cls, amount):
cls.raise_amount = amount

@staticmethod
def is_workday(day):
if day.weekday() == 5 or day.weekday() == 6:
return False
return True

def __repr__(self):
return "Employee('{}','{}',{})".format(self.first, self.last, self.pay)

def __str__(self):
return '{} - {}'.format(self.fullname(), self.email)

emp_1 = Employee('Bob', 'Gobdo', 20)
print(emp_1) # Bob Gobdo - Bob.Gobdo@company.com

print(emp_1.__repr__()) # Employee('Bob','Gobdo',20)

__add__ method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Employee:
raise_amount = 2.04

def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'

def fullname(self):
return '{} {}'.format(self.first, self.last)

def apply_raise(self):
self.pay = int(self.pay * self.raise_amount)

def set_regular_raise_amount(self, amount):
self.raise_amount = amount

@classmethod
def set_class_raise_amount(cls, amount):
cls.raise_amount = amount

@staticmethod
def is_workday(day):
if day.weekday() == 5 or day.weekday() == 6:
return False
return True

def __repr__(self):
return "Employee('{}','{}',{})".format(self.first, self.last, self.pay)

def __str__(self):
return '{} - {}'.format(self.fullname(), self.email)

def __add__(self, other):
return self.pay + other.pay

emp_1 = Employee('Bob', 'Gobdo', 20)
emp_2 = Employee('John', 'Youhs', 30)
print(emp_1 + emp_2) # 50

__len__ method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Employee:
raise_amount = 2.04

def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'

def fullname(self):
return '{} {}'.format(self.first, self.last)

def apply_raise(self):
self.pay = int(self.pay * self.raise_amount)

def set_regular_raise_amount(self, amount):
self.raise_amount = amount

@classmethod
def set_class_raise_amount(cls, amount):
cls.raise_amount = amount

@staticmethod
def is_workday(day):
if day.weekday() == 5 or day.weekday() == 6:
return False
return True

def __repr__(self):
return "Employee('{}','{}',{})".format(self.first, self.last, self.pay)

def __str__(self):
return '{} - {}'.format(self.fullname(), self.email)

def __add__(self, other):
return self.pay + other.pay

def __len__(self):
return len(self.fullname())

emp_1 = Employee('Bob', 'Gobdo', 20)
print(len(emp_1)) # 9