python3之神奇的property装饰器(实现原理)

学习python的同学,慢慢的都会接触到装饰器,装饰器在python里是功能强大的语法。装饰器配合python的魔法方法,能实现很多意想不到的功能。废话不多说,如果你已经掌握了闭包的原理,代码的逻辑还是可以看明白的,咱们直接进入正题。

property的意义

@property把一个类的getter方法变成属性,如果还有setter方法,就在setter方法前面加上@method.setter。使用类属性=property(getx,setx,delx,desc)也是可以的。

实现很简单,那么它背后的原理是什么呢?

Property类的伪代码如下,里面涉及了__get__、__set__、__delete__魔法方法。Decorator类是装饰器类,Target是目标类。当你设置装饰器类的实例对象为目标类的x属性后,当试图访问目标类的x属性会触发装饰器类的__get__方法;当为目标类的x属性赋值时,会触发装饰器类的__setter__方法;尝试删除目标类的x属性时,会触发装饰器类的__delete__方法。当访问Target.x.__doc__,可以打印出装饰器类的描述文档。事实上这种装饰器类也被称为描述符类。描述符类就是将一个特殊类的实例指派给一个类的属性。

类属性实现方式:

class Decorator(object):

    def __init__(self, fget=None, fset=None, fdel=None, doc=None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
        self.__doc__ = doc

    def __get__(self, instance, owner):
        if instance is None:
            return self
        return self.fget(instance)

    def __set__(self, instance, value):
        self.fset(instance, value)

    def __delete__(self, instance):
        self.fdel(instance)

    def getter(self, fget):
        return Decorator(fget, self.fset, self.fdel, self.__doc__)

    def setter(self, fset):
        return Decorator(self.fget, fset, self.fdel, self.__doc__)

    def deleter(self, fdel):
        return Decorator(self.fget, self.fset, fdel, self.__doc__)
        

class Target(object):
    
    desc = "Amazing pyhton"
    
    def __init__(self, attr=5):
        self._x = attr
    def getx(self):
        return self._x
    def setx(self, value):
        self._x = value
    def delx(self):
        del self._x

    x = Decorator(getx,setx,delx,desc)

装饰器实现方式:

class Decorator(object):

    def __init__(self, fget=None, fset=None, fdel=None, doc=None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
        self.__doc__ = doc

    def __get__(self, instance, owner):
        if instance is None:
            return self
        return self.fget(instance)

    def __set__(self, instance, value):
        self.fset(instance, value)

    def __delete__(self, instance):
        self.fdel(instance)

    def getter(self, fget):
        return Decorator(fget, self.fset, self.fdel, self.__doc__)

    def setter(self, fset):
        return Decorator(self.fget, fset, self.fdel, self.__doc__)

    def deleter(self, fdel):
        return Decorator(self.fget, self.fset, fdel, self.__doc__)


class Target(object):
    desc = "Amazing pyhton"

    def __init__(self, attr=5):
        self._x = attr
    @Decorator
    def show(self):
        return self._x

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

    @show.deleter
    def show(self):
        del self._x

 

展开阅读全文

没有更多推荐了,返回首页