-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathclassmethod_tutorial.py
67 lines (47 loc) · 1.63 KB
/
classmethod_tutorial.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
62
63
64
65
66
67
# ref. https://www.programiz.com/python-programming/methods/built-in/classmethod
# Static method vs Class method
# Static method knows nothing about the class and just deals with the parameters.
# Class method works with the class since its parameter is always the class itself.
from datetime import date
# Create factory method using class method
# random Person
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@classmethod
def from_birth_year(cls, name, birth_year):
return cls(name, date.today().year - birth_year)
def display(self):
print(self._name + "'s age is: " + str(self._age))
# How class method works for inheritance?
# random Person
class Person2:
def __init__(self, name, age):
self._name = name
self._age = age
@staticmethod
def from_fathers_age(name, father_age, father_person_age_diff):
return Person2(name, date.today().year - father_age + father_person_age_diff)
@classmethod
def from_birth_year(cls, name, birth_year):
return cls(name, date.today().year - birth_year)
def display(self):
print(self._name + "'s age is: " + str(self._age))
class Man(Person2):
sex = 'Male'
def main():
# tutorial 1
person = Person('Adam', 19)
person.display()
person1 = Person.from_birth_year('John', 1985)
person1.display()
# tutorial 2
man = Man.from_birth_year('John', 1985)
print(isinstance(man, Man))
man.display()
man1 = Man.from_fathers_age('John', 1965, 20)
print(isinstance(man1, Man))
man1.display()
if __name__ == "__main__":
main()