>  기사  >  백엔드 개발  >  Python의 매직 설명자

Python의 매직 설명자

巴扎黑
巴扎黑원래의
2017-03-18 11:36:071360검색

소개

설명자(descriptor)는 Python 언어에서 심오하지만 중요한 흑마술입니다. Python 언어의 핵심으로 널리 사용됩니다. 설명자에 능숙하면 Python 추가됩니다. 프로그래머의 도구 상자에 속임수를 쓰세요. 이 글에서는 디스크립터의 정의와 몇 가지 일반적인 시나리오에 대해 이야기하고, 글 마지막에 , __getattr, __getattribute__ 세 가지 __getitem__매직 메소드를 추가하겠습니다. 속성 액세스를 포함합니다.

설명자 정의

descr__get__(self, obj, objtype=None) --> value
descr.__set__(self, obj, value) --> None
descr.__delete__(self, obj) --> None

(객체 속성)가 위의 세 가지 메서드 중 하나를 정의하는 한 이 클래스는 설명자 클래스라고 부를 수 있습니다. object attribute

설명자 기본

다음 예에서는

클래스를 만들고 RevealAcess 메서드를 구현합니다. 이제 이 클래스를 설명자 클래스라고 부를 수 있습니다. __get__

class RevealAccess(object):
    def __get__(self, obj, objtype):
        print('self in RevealAccess: {}'.format(self))
        print('self: {}\nobj: {}\nobjtype: {}'.format(self, obj, objtype))
class MyClass(object):
    x = RevealAccess()
    def test(self):
        print('self in MyClass: {}'.format(self))

EX1 인스턴스 속성

다음으로

메소드의 각 매개변수의 의미를 살펴보겠습니다. 다음 예에서 __get__는 RevealAccess 클래스 인스턴스 x, self는 MyClass 클래스의 인스턴스 m이고, obj는 이름에서 알 수 있듯이 MyClass 클래스 자체입니다. 출력 문에서 볼 수 있듯이 objtype 액세스 설명자 m.xx 메서드를 호출합니다. __get__

>>> m = MyClass()
>>> m.test()
self in MyClass: <__main__.MyClass object at 0x7f19d4e42160>
>>> m.x
self in RevealAccess: <__main__.RevealAccess object at 0x7f19d4e420f0>
self: <__main__.RevealAccess object at 0x7f19d4e420f0>
obj: <__main__.MyClass object at 0x7f19d4e42160>
objtype: <class &#39;__main__.MyClass&#39;>

EX2 클래스 속성

속성이 클래스를 통해 직접 액세스되는 경우 x 연결은 직접 None이므로 이해하기 쉽습니다. , MyClass 인스턴스가 존재하지 않기 때문입니다. obj

>>> MyClass.x
self in RevealAccess: <__main__.RevealAccess object at 0x7f53651070f0>
self: <__main__.RevealAccess object at 0x7f53651070f0>
obj: None
objtype: <class &#39;__main__.MyClass&#39;>

설명자 원리

설명자 트리거

위의 예에서는 각각 인스턴스 속성과 클래스 속성의 관점에서 설명자의 사용법을 나열했습니다. 내부 원리 분석:

  • 에 액세스하면 기본 클래스 객체의 __getattribute__ 메서드가 실제로 호출됩니다. 이 메서드에서는 obj.d가 实例属性로 변환됩니다. type(obj).__dict__['d'].__get__(obj, type(obj))

  • 에 액세스하면 cls.d를 类属性로 변환하는 메타클래스 유형의 __getattribute__ 메서드를 호출하는 것과 같습니다. 여기서 __get__( )의 obj는 다음과 같습니다. 인스턴스가 없기 때문에 없음입니다. cls.__dict__['d'].__get__(None, cls)

매직 메소드에 대해 간단히 이야기해보겠습니다. 이 메소드는 객체의 속성에 접근할 때 무조건 호출됩니다. 자세한 내용은 __getattribute__, __getattr 과 같습니다. 기사 마지막 부분에 추가 보충 자료를 작성하겠지만 지금은 자세히 다루지 않겠습니다. __getitem__

설명자 우선순위

먼저 설명자는 두 가지 유형으로 나뉩니다.

  • 객체가 __get__() 및 __set__() 메서드를 모두 정의하는 경우 , 이 설명자를

    이라고 합니다. data descriptor

  • 객체가 __get__() 메서드만 정의하는 경우 이 설명자를

    이라고 합니다. non-data descriptor

속성에 액세스할 때 네 가지 상황이 있습니다.

  • 데이터 설명자

  • 인스턴스 사전

  • 비데이터 설명자

  • __getattr__()

우선순위 크기는

data descriptor > instance dict > non-data descriptor > __getattr__()

이게 무슨 뜻인가요? 즉, 동일한 이름을 가진

data descriptor->d가 인스턴스 객체 obj에 나타나면 instance attribute->dobj.d 속성에 액세스할 때 데이터 설명자의 우선순위가 더 높기 때문에 Python은 d을 호출합니다. 대신 obj.__dict__['d']를 호출하는 대신. 그러나 설명자가 데이터 설명자가 아닌 경우 Python은 type(obj).__dict__['d'].__get__(obj, type(obj))을 호출합니다. obj.__dict__['d']

속성

설명자를 사용할 때마다 설명자 클래스를 정의하는 것은 매우 번거로운 작업 같습니다. Python은 속성에 데이터 설명자를 추가하는 간결한 방법을 제공합니다.

property(fget=None, fset=None, fdel=None, doc=None) -> property attribute

fget, fset 및 fdel은 각각 클래스의 getter, setter 및 deleter 메서드입니다. 다음 예를 사용하여 속성 사용 방법을 설명합니다.

class Account(object):
    def __init__(self):
        self._acct_num = None
    def get_acct_num(self):
        return self._acct_num
    def set_acct_num(self, value):
        self._acct_num = value
    def del_acct_num(self):
        del self._acct_num
    acct_num = property(get_acct_num, set_acct_num, del_acct_num, &#39;_acct_num property.&#39;)

acct가 Account의 인스턴스인 경우 acct.acct_num은 getter를 호출하고 acct.acct_num = value는 setter를 호출하며 del acct_num.acct_num 삭제자를 호출합니다.

>>> acct = Account()
>>> acct.acct_num = 1000
>>> acct.acct_num
1000

Python은 간단한 애플리케이션 시나리오를 위한 속성을 생성하는 데 사용할 수 있는

데코레이터도 제공합니다. 속성 개체에는 해당 데코레이팅된 함수의 접근자 함수를 통해 속성의 복사본을 만드는 데 사용할 수 있는 getter, setter 및 delete 데코레이터 메서드가 있습니다. @property

class Account(object):
    def __init__(self):
        self._acct_num = None
    @property
     # the _acct_num property. the decorator creates a read-only property
    def acct_num(self):
        return self._acct_num
    @acct_num.setter
    # the _acct_num property setter makes the property writeable
    def set_acct_num(self, value):
        self._acct_num = value
    @acct_num.deleter
    def del_acct_num(self):
        del self._acct_num

속성을 읽기 전용으로 설정하려면 setter 메소드를 제거하면 됩니다.

런타임에 설명자 만들기

런타임에 속성을 추가할 수 있습니다:

class Person(object):
    def addProperty(self, attribute):
        # create local setter and getter with a particular attribute name
        getter = lambda self: self._getProperty(attribute)
        setter = lambda self, value: self._setProperty(attribute, value)
        # construct property attribute and add it to the class
        setattr(self.__class__, attribute, property(fget=getter, \
                                                    fset=setter, \
                                                    doc="Auto-generated method"))
    def _setProperty(self, attribute, value):
        print("Setting: {} = {}".format(attribute, value))
        setattr(self, &#39;_&#39; + attribute, value.title())
    def _getProperty(self, attribute):
        print("Getting: {}".format(attribute))
        return getattr(self, &#39;_&#39; + attribute)
rrree

정적 메서드 및 클래스 메서드

설명자를 사용하여 구현을 시뮬레이션할 수 있습니다. Python의

@staticmethod. 먼저 아래 표를 살펴보겠습니다. @classmethod

Transformation Called from an Object Called from a Class
function f(obj, *args) f(*args)
staticmethod f(*args) f(*args)
classmethod f(type(obj), *args) f(klass, *args)

静态方法

对于静态方法fc.fC.f是等价的,都是直接查询object.__getattribute__(c, ‘f’)或者object.__getattribute__(C, ’f‘)。静态方法一个明显的特征就是没有self变量。

静态方法有什么用呢?假设有一个处理专门数据的容器类,它提供了一些方法来求平均数,中位数等统计数据方式,这些方法都是要依赖于相应的数据的。但是类中可能还有一些方法,并不依赖这些数据,这个时候我们可以将这些方法声明为静态方法,同时这也可以提高代码的可读性。

使用非数据描述符来模拟一下静态方法的实现:

class StaticMethod(object):
    def __init__(self, f):
        self.f = f
    def __get__(self, obj, objtype=None):
        return self.f

我们来应用一下:

class MyClass(object):
    @StaticMethod
    def get_x(x):
        return x
print(MyClass.get_x(100))  # output: 100

类方法

Python的@classmethod@staticmethod的用法有些类似,但是还是有些不同,当某些方法只需要得到类的引用而不关心类中的相应的数据的时候就需要使用classmethod了。

使用非数据描述符来模拟一下类方法的实现:

class ClassMethod(object):
    def __init__(self, f):
        self.f = f
    def __get__(self, obj, klass=None):
        if klass is None:
            klass = type(obj)
        def newfunc(*args):
            return self.f(klass, *args)
        return newfunc

其他的魔术方法

首次接触Python魔术方法的时候,我也被__get____getattribute____getattr____getitem__之间的区别困扰到了,它们都是和属性访问相关的魔术方法,其中重写__getattr____getitem__来构造一个自己的集合类非常的常用,下面我们就通过一些例子来看一下它们的应用。

__getattr__

Python默认访问类/实例的某个属性都是通过__getattribute__来调用的,__getattribute__会被无条件调用,没有找到的话就会调用__getattr__。如果我们要定制某个类,通常情况下我们不应该重写__getattribute__,而是应该重写__getattr__,很少看见重写__getattribute__的情况。

从下面的输出可以看出,当一个属性通过__getattribute__无法找到的时候会调用__getattr__

In [1]: class Test(object):
    ...:     def __getattribute__(self, item):
    ...:         print(&#39;call __getattribute__&#39;)
    ...:         return super(Test, self).__getattribute__(item)
    ...:     def __getattr__(self, item):
    ...:         return &#39;call __getattr__&#39;
    ...:
In [2]: Test().a
call __getattribute__
Out[2]: &#39;call __getattr__&#39;

应用

对于默认的字典,Python只支持以obj['foo']形式来访问,不支持obj.foo的形式,我们可以通过重写__getattr__让字典也支持obj['foo']的访问形式,这是一个非常经典常用的用法:

class Storage(dict):
    """
    A Storage object is like a dictionary except `obj.foo` can be used
    in addition to `obj[&#39;foo&#39;]`.
    """
    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError as k:
            raise AttributeError(k)
    def __setattr__(self, key, value):
        self[key] = value
    def __delattr__(self, key):
        try:
            del self[key]
        except KeyError as k:
            raise AttributeError(k)
    def __repr__(self):
        return &#39;<Storage &#39; + dict.__repr__(self) + &#39;>&#39;

我们来使用一下我们自定义的加强版字典:

>>> s = Storage(a=1)
>>> s[&#39;a&#39;]
1
>>> s.a
1
>>> s.a = 2
>>> s[&#39;a&#39;]
2
>>> del s.a
>>> s.a
...
AttributeError: &#39;a&#39;

__getitem__

getitem用于通过下标[]的形式来获取对象中的元素,下面我们通过重写__getitem__来实现一个自己的list。

class MyList(object):
    def __init__(self, *args):
        self.numbers = args
    def __getitem__(self, item):
        return self.numbers[item]
my_list = MyList(1, 2, 3, 4, 6, 5, 3)
print my_list[2]

这个实现非常的简陋,不支持slice和step等功能,请读者自行改进,这里我就不重复了。

应用

下面是参考requests库中对于__getitem__的一个使用,我们定制了一个忽略属性大小写的字典类。

程序有些复杂,我稍微解释一下:由于这里比较简单,没有使用描述符的需求,所以使用了@property装饰器来代替,lower_keys的功能是将实例字典中的键全部转换成小写并且存储在字典self._lower_keys中。重写了__getitem__方法,以后我们访问某个属性首先会将键转换为小写的方式,然后并不会直接访问实例字典,而是会访问字典self._lower_keys去查找。赋值/删除操作的时候由于实例字典会进行变更,为了保持self._lower_keys和实例字典同步,首先清除self._lower_keys的内容,以后我们重新查找键的时候再调用__getitem__的时候会重新新建一个self._lower_keys

class CaseInsensitiveDict(dict):
    @property
    def lower_keys(self):
        if not hasattr(self, &#39;_lower_keys&#39;) or not self._lower_keys:
            self._lower_keys = dict((k.lower(), k) for k in self.keys())
        return self._lower_keys
    def _clear_lower_keys(self):
        if hasattr(self, &#39;_lower_keys&#39;):
            self._lower_keys.clear()
    def __contains__(self, key):
        return key.lower() in self.lower_keys
    def __getitem__(self, key):
        if key in self:
            return dict.__getitem__(self, self.lower_keys[key.lower()])
    def __setitem__(self, key, value):
        dict.__setitem__(self, key, value)
        self._clear_lower_keys()
    def __delitem__(self, key):
        dict.__delitem__(self, key)
        self._lower_keys.clear()
    def get(self, key, default=None):
        if key in self:
            return self[key]
        else:
            return default

我们来调用一下这个类:

>>> d = CaseInsensitiveDict()
>>> d[&#39;ziwenxie&#39;] = &#39;ziwenxie&#39;
>>> d[&#39;ZiWenXie&#39;] = &#39;ZiWenXie&#39;
>>> print(d)
{&#39;ZiWenXie&#39;: &#39;ziwenxie&#39;, &#39;ziwenxie&#39;: &#39;ziwenxie&#39;}
>>> print(d[&#39;ziwenxie&#39;])
ziwenxie
# d[&#39;ZiWenXie&#39;] => d[&#39;ziwenxie&#39;]
>>> print(d[&#39;ZiWenXie&#39;])
ziwenxie

위 내용은 Python의 매직 설명자의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.