Skip to content

Instantly share code, notes, and snippets.

@justxor
Created June 1, 2022 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justxor/c9c5f0f2f05fbbcd251a6dbf86b07be8 to your computer and use it in GitHub Desktop.
Save justxor/c9c5f0f2f05fbbcd251a6dbf86b07be8 to your computer and use it in GitHub Desktop.
from datetime import date
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# a class method to create a Person object by birth year.
@classmethod
def fromBirthYear(cls, name, year):
return cls(name, date.today().year - year)
# a static method to check if a Person is adult or not.
@staticmethod
def isAdult(age):
return age > 18
person1 = Person('mayank', 21)
person2 = Person.fromBirthYear('mayank', 1996)
print (person1.age)
print (person2.age)
# print the result
print (Person.isAdult(22))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment