리자

@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년 전 조회 856
2년 전 조회 678
2년 전 조회 674
2년 전 조회 683
2년 전 조회 895
2년 전 조회 748
2년 전 조회 1,133
2년 전 조회 884
2년 전 조회 879
2년 전 조회 1,157
2년 전 조회 1,071
2년 전 조회 943
3년 전 조회 1,168
3년 전 조회 1,266
3년 전 조회 1,320
3년 전 조회 1,338
4년 전 조회 1,241
4년 전 조회 1,535
4년 전 조회 1,462
4년 전 조회 1,676
4년 전 조회 1,458
4년 전 조회 2,740
4년 전 조회 1,450
4년 전 조회 1,272
5년 전 조회 1,390
5년 전 조회 1,830
5년 전 조회 1,834
5년 전 조회 1,564
5년 전 조회 1,746
5년 전 조회 1,731