본문 바로가기
파이썬

파이썬 How to set attributes using property decorators?

by º기록 2021. 1. 16.
반응형

이 코드는 오류를 반환합니다. AttributeError : ca n't set attribute 메서드를 호출하는 대신 속성을 사용하고 싶기 때문에 이것은 정말 유감입니다. 이 간단한 예제가 작동하지 않는 이유를 아는 사람이 있습니까?

#!/usr/bin/python2.6


class Bar( object ):
    """ 
    ...
    """

    @property
    def value():
      """
      ...
      """    
      def fget( self ):
          return self._value

      def fset(self, value ):
          self._value = value


class Foo( object ):
    def __init__( self ):
        self.bar = Bar()
        self.bar.value = "yyy"

if __name__ == '__main__':
    foo = Foo()

 

해결 방법

 

이것이 당신이 원하는 것입니까?

class C(object):
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value


 

참조 페이지 https://stackoverflow.com/questions/1684828

 

 

반응형

댓글