리자

@classmethod 완벽 이해

· 2023-07-12 (수) 10:14:14 · 1095

이해가 잘 안돼서 찾아보다가 발견한 동영상 입니다.

 

몇번 보니 이해가 조금(?) 되네요. (머리가 나빠서요 ㅎ)

 

https://youtu.be/MP-MXkQPrEc

 

 

 

[code]

class Member():
    _ins = []

 

    def __init__(self, name, height, weight, fat):
        self.name = name
        self.height = height
        self.weight = weight
        self.fat = fat

        self.add_instance(self)

 

    @classmethod
    def add_instance(cls, ins):
        cls._ins.append(ins)

 

a = Member("kim", 180, 77, 24)
b = Member("Ihm", 170, 71, 16)
c = Member("Choi", 160, 51, 23)
d = Member("Park", 170, 63, 20)

 

height_mean = sum([m.height for m in Member._ins]) / len(Member._ins)
print(f"height_mean = {height_mean}")

[/code]

|
댓글을 작성하시려면 로그인이 필요합니다.
+
제목 글쓴이 날짜 조회
19-12-19 조회 1,772
19-12-18 조회 1,737
19-12-17 조회 1,662
19-12-16 조회 1,328
19-12-15 조회 1,464
19-12-14 조회 1,368
19-12-13 조회 1,706
19-12-13 조회 1,619
19-12-12 조회 1,294
19-12-12 조회 1,083
19-12-12 조회 1,347
19-12-11 조회 1,990
19-12-11 조회 1,447
19-12-10 조회 1,147
19-12-10 조회 1,533
19-12-10 조회 1,138
19-12-09 조회 1,387
19-12-07 조회 1,599
19-12-07 조회 1,376
19-12-06 조회 1,773
19-12-06 조회 1,427
19-12-05 조회 1,372
19-12-05 조회 1,287
19-12-04 조회 2,689
19-12-04 조회 1,200
19-12-03 조회 2,755
19-12-03 조회 1,191
19-12-03 조회 1,295
19-11-19 조회 1,374
19-11-19 조회 1,571