리자

@classmethod 완벽 이해

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

 

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

 

 

 

 

[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]

|
댓글을 작성하시려면 로그인이 필요합니다.

파이썬 게시판 만들기

+
제목 글쓴이 날짜 조회
2년 전 조회 833
2년 전 조회 653
2년 전 조회 636
2년 전 조회 660
2년 전 조회 869
2년 전 조회 719
2년 전 조회 1,100
2년 전 조회 846
2년 전 조회 833
2년 전 조회 1,119
2년 전 조회 1,044
2년 전 조회 916
3년 전 조회 1,135
3년 전 조회 1,236
3년 전 조회 1,293
3년 전 조회 1,292
4년 전 조회 1,211
4년 전 조회 1,501
4년 전 조회 1,436
4년 전 조회 1,651
4년 전 조회 1,423
4년 전 조회 2,700
4년 전 조회 1,417
4년 전 조회 1,236
4년 전 조회 1,362
5년 전 조회 1,801
5년 전 조회 1,793
5년 전 조회 1,529
5년 전 조회 1,699
5년 전 조회 1,689