programing

Python 속성은 어떻게 작동합니까?

nasanasas 2020. 11. 2. 08:03
반응형

Python 속성은 어떻게 작동합니까?


Python 속성을 성공적으로 사용했지만 어떻게 작동하는지 모르겠습니다. 클래스 외부의 속성을 역 참조하면 property다음 유형의 객체를 얻습니다 .

@property
def hello(): return "Hello, world!"

hello  # <property object at 0x9870a8>

그러나 클래스에 속성을 넣으면 동작이 매우 다릅니다.

class Foo(object):
   @property
   def hello(self): return "Hello, world!"

Foo().hello # 'Hello, world!'

언 바운드 Foo.hello가 여전히 property객체 라는 것을 알았 으므로 클래스 인스턴스화가 마법을 수행해야하지만 그 마법은 무엇입니까?


다른 사람들이 언급했듯이 설명 자라는 언어 기능을 사용합니다.

클래스를 통해 액세스 할 때 실제 속성 개체가 반환되는 이유 Foo.hello는 속성이 __get__(self, instance, owner)특수 메서드를 구현하는 방법에 있습니다.

  • 설명자가 인스턴스 에서 액세스되면 해당 인스턴스가 적절한 인수로 전달되고 해당 인스턴스 owner클래스 입니다.
  • 클래스를 통해 액세스하면 instanceNone이고 만 owner전달됩니다. property객체는이 반환을 인식합니다 self.

Descriptors howto 외에도 Language Guide의 Implementing DescriptorsInvoking Descriptors 문서를 참조하세요 .


@properties가 제대로 작동하려면 클래스가 object 의 하위 클래스 여야 합니다 . 클래스가 객체 의 하위 클래스가 아닐 때 처음으로 setter에 액세스하려고하면 실제로 setter를 통해 액세스하는 대신 더 짧은 이름의 새 속성을 만듭니다.

다음은 올바르게 작동 하지 않습니다 .

class C(): # <-- Notice that object is missing

    def __init__(self):
        self._x = None

    @property
    def x(self):
        print 'getting value of x'
        return self._x

    @x.setter
    def x(self, x):
        print 'setting value of x'
        self._x = x

>>> c = C()
>>> c.x = 1
>>> print c.x, c._x
1 0

다음은 올바르게 작동합니다.

class C(object):

    def __init__(self):
        self._x = None

    @property
    def x(self):
        print 'getting value of x'
        return self._x

    @x.setter
    def x(self, x):
        print 'setting value of x'
        self._x = x

>>> c = C()
>>> c.x = 1
setting value of x
>>> print c.x, c._x
getting value of x
1 1

속성은 설명자 이며 설명자는 클래스 인스턴스의 구성원 일 때 특별히 작동합니다. 즉,이 경우 a타입의 인스턴스 AA.foo디스크립터는 다음이다 a.foo동등하다 A.foo.__get__(a).


property목적은 단지 설명 프로토콜을 구현 : http://docs.python.org/howto/descriptor.html

참고URL : https://stackoverflow.com/questions/6193556/how-do-python-properties-work

반응형