Skip to content

Commit 2363068

Browse files
committed
class methods
1 parent 5f3fe73 commit 2363068

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

class-methods.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from datetime import date
2+
3+
class Employee:
4+
def __init__(self,name,age):
5+
self.age=age
6+
self.name=name
7+
8+
9+
def displayInfo(self):
10+
return f"Name {self.name} age {self.age}"
11+
12+
@classmethod
13+
def fromBirthYear(cls,name,birth_year):
14+
return cls(name=name,age=(date.today().year-birth_year))
15+
16+
17+
emp1=Employee(name="Jonathan",age=23)
18+
emp2=Employee.fromBirthYear(name="Jerry",birth_year=1985)
19+
20+
21+
print(emp1.displayInfo())
22+
print(emp2.displayInfo())
23+
24+
25+
26+

0 commit comments

Comments
 (0)