UFO ET IT

속성 데코레이터를 사용하여 속성을 설정하는 방법은 무엇입니까?

ufoet 2020. 12. 3. 21:09
반응형

속성 데코레이터를 사용하여 속성을 설정하는 방법은 무엇입니까?


이 코드는 오류를 반환합니다. 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

http://docs.python.org/library/functions.html#property 에서 가져 왔습니다 .

참고 URL : https://stackoverflow.com/questions/1684828/how-to-set-attributes-using-property-decorators

반응형