• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

클래스 변수

19.11.26 18:12 작성 조회수 84

0

클래스 변수는 C++로 치면 public에 선언된 static 멤버 변수라고 생각하면 되나요?

답변 1

답변을 작성해보세요.

0

좋은 질문입니다.

약간 다르다고 생각하셔야 될 것 같습니다.

파이썬 클래스 변수는 모든 인스턴스 들이공유하지만, 수정이 가능한 변수입니다. 즉, 초기화 후 정적인 변수가 아닙니다.

그래서 python은 java, c++ 과 같이 정적인 변수가 존재하지 않고, @property 등을 통해서 구현해야 될 것 같습니다.

지금 질문 주신것에 대한 가장 활발하게 토론이 되고 있는 링크를 보내드립니다. 번역해서 읽으시면 꽤 잼나게 읽으실 수 

있을 거예요.

https://stackoverflow.com/questions/68645/are-static-class-variables-possible-in-python

"Static Variables"

However, implementing "static variables" (well, mutable static variables, anyway, if that's not a contradiction in terms...) is not as straight forward. As millerdev pointed out in his answer, the problem is that Python's class attributes are not truly "static variables". Consider: