• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

logging print 순서에 대한 질문입니다.

23.10.09 19:25 작성 23.10.09 19:25 수정 조회수 144

0

안녕하세요 수업을 따라하던 도중

import logging

logging.basicConfig(
                    format='%(asctime)s %(message)s',
                    level=logging.INFO,
                    datefmt='%Y-%m-%d %H:%M:%S'
)

class LoggedScoreAccess:
    def __init__(self, value=50):
        self.value = value

    def __get__(self, obj, objtype=None ):
        logging.info('Aceesing %r giving %r','score',self.value)
        return self.value

    def __set__(self, obj, value):
        logging.info('Updating %r giving %r','score',self.value)
        self.value = value



class student:
    # Descriptor instance
    score = LoggedScoreAccess()

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


s1 = student("Kim")
s2 = student("Lee")

# 점수 확인(s1)
print('Ex2 > ', s1.score)
s1.score += 20
# print('Ex2 > ', s1.score)

 

다음과 같은 코드를 작성했는데

제 생각으로는 print('Ex2 > ', s1.score) 이후에

s1.score += 20

실행이 되었기 때문에

로그 순서가 수업과 같이 나오는게 맞다고 생각하는데

 

실제 print 되는 순서는

2023-10-09 19:23:54 Aceesing 'score' giving 50 2023-10-09 19:23:54 Aceesing 'score' giving 50 2023-10-09 19:23:54 Updating 'score' giving 50

Ex2 > 50

다음과 같이

log가 모두 출력된 후에 print가 나오게 됩니다.

 

어떻게 된건지 알 수 있을까요?

답변 1

답변을 작성해보세요.

0

안녕하세요.

아래 부분까지 주석 해제하시고 실행하시면 이 순서가 맞습니다.

최초의 variable initialize -> 값 확인(get) -> access 출력

get-> updating -> access 출력입니다.

 

2023-10-10 00:31:08 Aceesing 'score' giving 50

Ex2 > 50

2023-10-10 00:31:08 Aceesing 'score' giving 50

2023-10-10 00:31:08 Updating 'score' giving 50

2023-10-10 00:31:08 Aceesing 'score' giving 70

Ex2 > 70

>