강의

멘토링

커뮤니티

Inflearn コミュニティ Q&A

yyh36902419 のプロフィール画像
yyh36902419

投稿した質問数

Python for Everyone: 必須文法を学ぶ Feat. オープンソースパッケージ配布 (Inflearn Original)

Descriptor(2)

descriptor 마지막 예제 관련 질문있습니다.

作成

·

180

1

- 학습 관련 질문을 남겨주세요. 상세히 작성하면 더 좋아요!
- 먼저 유사한 질문이 있었는지 검색해보세요.
- 서로 예의를 지키며 존중하는 문화를 만들어가요.
- 잠깐! 인프런 서비스 운영 관련 문의는 1:1 문의하기를 이용해주세요.

 

마지막 예제에서 기본 점수였던 50이

s1은 20점을 올려 70이 출력되고

s2는 30점을 올리면 80이 출력이 되야하는데

 

s1의 setter가 끝난 value을 s2가 get 하는 것 같습니다.

 

이 오류는 어떻게 해결해야할지 궁금합니다.

python

回答 2

0

class LoggedScoreAccess:
    def __init__(self):
        self.value = None

    def __get__(self, obj, objtype=None):
        logging.info(f"Accessing {'score'} gibing {self.value}")
        return self.value

    def __set__(self, obj, value):
        logging.info(f"Updating {'score'} gibing {value}")
        self.value = value


class Student:
    # Descriptor instance
    score = LoggedScoreAccess()

    def __init__(self, name, score):
        self.score = score
        # Regular instance attribute
        self.name = name


s1 = Student("Kim", 50)
s2 = Student("Lee", 30)

# 점수 확인 (s1)
print(f"Ex2 s1 > {s1.score}")
s1.score += 20
print(f"Ex2 s1 > {s1.score}")

# 점수 확인 (s2)
print(f"Ex2 s2 > {s2.score}")
s2.score += 30
print(f"Ex2 s2 > {s2.score}")

0

niceman님의 프로필 이미지
niceman
インストラクター

안녕하세요.

같은 변수를 참조하고 있지 않은지 선언 스코프에 따라서 

한 값을 참조 하고 있는 것 같습니다.

확인 해보세요!

전체 소스를 확인해보시고 안되면 소스를 붙여넣기 해주세여~

감사합니다.

26:00

기대한 결과값이 출력 되지 않네요

yyh36902419 のプロフィール画像
yyh36902419

投稿した質問数

質問する