This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_methods.py
61 lines (45 loc) · 1.99 KB
/
class_methods.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
""" Introduction to Python class methods """
class Person:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def get_full_name(self):
return f'{self.first_name} {self.last_name}'
def introduce(self):
return f'Hey, I am {self.get_full_name()} and I am {self.age} years old!'
# Suppose that you want to add a method that creates an anonymous person to the Person class.
class Person:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def get_full_name(self):
return f'{self.first_name} {self.last_name}'
def introduce(self):
return f'Hey, I am {self.get_full_name()} and I am {self.age} years old!'
def anonymous(self):
return Person('Asad', 'Hussain', 20)
# However, to invoke the create_anonymous() method, you need to create an instance,
# which doesn’t make sense in this case.
# This is why Python class methods come into play.
class Person:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def get_full_name(self):
return f'{self.first_name} {self.last_name}'
def introduce(self):
return f'Hey, I am {self.get_full_name()} and I am {self.age} years old!'
@classmethod
def anonymous(cls):
return Person('Asad', 'Hussain', 20)
# The anonymous() method cannot access instance attributes. But it can access class attributes via the cls variable.
""" Calling Python class methods """
anonymous = Person.anonymous()
print(anonymous.introduce())
""" Summary """
# Python class methods aren’t bound to any specific instance, but classes.
# Use @classmethod decorator to change an instance method to a class method. Also, pass the cls as the first parameter to the class method.
# Use class methods for factory methods.