6.10 Python object-oriented (10): property decorator

@ overview

  • The function of property decorator is to integrate all kinds of property accessors (setx, getx,delx) into one to help us lazy
  • The original c.setx(value) is simplified to c.x = value with decorator
  • The original print(c.getx()) is simplified to print(c.x) with decorator
  • The original c.del x () is simplified to del c.x after adding a decorator
  • The function of property accessor is to ensure data security. For unclear information, please refer to Property accessor and data security

Property is a class provided by the standard library. A property instance maps a common class property. When creating a property, we can specify various accessors for its corresponding property. Here is a revision of the official example

# An improved version of the official source code case
class C(object): 

    # Accessor (we can set the corresponding access right here, etc.)
    def getx(self):
        print("Accessor: please show me your ID card!")
        return self.__x

    # Setter (we can verify the validity of data here, etc.)
    def setx(self, value):
        # Here, the value of x must be greater than 0, otherwise an exception will be thrown
        if value < 0:
            raise ValueError("a positive x required")
        self.__x = value

    # Remover (we can set the corresponding access rights here)
    def delx(self):
        # You must enter the correct password to delete
        pwd = input("please enter the admin pwd:")
        if pwd == "123456":
            del self.__x
            print("x deleted!")
        else:
            print("Wrong password, fuck off!")

    # How to create a property instance, declare the property and its various "devices"
    x = property(fget=getx, fset=setx, fdel=delx, doc="I'm the 'x' property.")

Now the usual getx,setx,delx of the \

    # Test properties set as property instances
    c = C()
    c.x = 12  # setx
    print(c.x)  # getx
    del c.x  # Call delx
    # print(c.x) # Report AttributeError to prove the deletion is successful

results of enforcement

We can also define the accessor more simply and intuitively through the property decorator. The above case can be rewritten as

# An improved version of the official source code case
# In the way of property decorator, declare property and its various "devices"
class C2(object):

    # Property declaration can be used as an accessor when there is no accessor
    @property
    def x(self):
        return self.__x

    # Accessor (we can set the corresponding access right here, etc.)
    @x.getter
    def x(self):
        "I'm the 'x' getter."
        print("Accessor: Reception VIP at the front desk!")
        return self.__x

    # Setter (we can verify the validity of data here, etc.)
    @x.setter
    def x(self, value):
        # Here, the value of x must be greater than 0, otherwise an exception will be thrown
        if value < 0:
            raise ValueError("a positive x required")
        self.__x = value

    # Remover (we can set the corresponding access rights here)
    @x.deleter
    def x(self):
        # You must enter the correct password to delete
        pwd = input("please enter the admin pwd:")
        if pwd == "123456":
            del self.__x
            print("x deleted!")
        else:
            print("fuck off!")

The setting, obtaining and deleting of the \\\\\\\\\\

    # Using the decorator version
    c2 = C2()
    # c2.x = -10 # ValueError: a positive x required
    c2.x = 10  # Call the function decorated by x.setter
    print(c2.x)  # Call the function decorated by x.getter
    del c2.x  # Call the function decorated by x.delete
    # print(c2.x)  # Report AttributeError to prove the deletion is successful

results of enforcement

Posted by icey37 on Sat, 04 Apr 2020 06:17:41 -0700