5-11 Class Method

Grace Goh·2022년 11월 16일
0

Django Rest Framework

목록 보기
28/36
from datetime import date


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    @classmethod
    def fromBirthYear(cls, name, birthYear):
        return cls(name, date.today().year - birthYear)
        # Person(name, age올해-출생연도)

    def display(self):
        print(self.name + "'s age is: " + str(self.age))



me = Person('Grace', 35)
me.display()
# 인스턴스.method

# 인스턴스 = class.method
you = Person.fromBirthYear('Rafa', 1986)
you.display()

Grace's age is: 35
Rafa's age is: 36

profile
Español, Inglés, Coreano y Python

0개의 댓글