There are many built-in decorators inside Python, and they all have special features that are summarized below.
Series Articles
A single-instance pattern of Python design patterns (i)
Summary of common creation patterns of Python design patterns (ii.)
Python design pattern Decorator details (iii)
Python design mode with built-in adorner (iv)
Python design pattern iterator and builder details (v)
Python Decorator Introduction
- Reference: www.cnblogs.com/cwp-bg/p/9547797.html
Python comes with decorator Staticmethod
The function of the Staticmethod adorner is to remove the class by default the first parameter is an instance of the class, making the method a normal function, and Staticmethod is a class that belongs to the class decorator.
class Person(object): def eat(self): print('eat thing') @staticmethod def go(): # 不再传递self print('go')
Classmethod
When a class is defined, the internal method defaults to the method of the instance object of the class, and the Classmethod adorner makes the defined method a class method, and the first parameter of the class method is the class itself; The calling class method does not need to create an instance of the class. Classmethod is also a class, so Classmethod is a class decorator.
class Person(object): _num_ear = 2 def eat(self): print('eat thing') @classmethod def go(cls): print(cls._num_ear) print('go')if __name__ == '__main__': Person.go() # 无需创建实例,直接调用。
Property
There is no limit to the access of Python for a property of a class, but sometimes we need to restrict the access to the property, which is what the decorator is doing.
property is a class, it has three methods, Deleter,setter,getter, there are two ways to use.
class Person(Animal): _num_ear = 2 def __init__(self): self._name = 'xiaoming' self.age = 20 def get_name(self): print('get name') return self._name def set_name(self, name): print('set name') self._name = name def delete_name(self): print('del name') del self._name name = property(get_name, set_name, delete_name, doc='name of person')if __name__ == '__main__': p = Person() print(p.name) # 会调用get_name p.name = 'xxxx' # 会调用set_name del p.name # 会调用delete_name
Property can manually specify the limit of the function, there are four parameters, but this is more cumbersome, you can use the form of adorners.
class Person(Animal): _num_ear = 2 @property def name(self): return self._name @name.setter def name(self, nm): self._name = nm @name.deleter def name(self): del self._nameif __name__ == '__main__': p = Person() print(p.name) p.name = 'xxxx' del p.name
When a function is decorated, the Property object is returned, and only the function of the Fget parameter can be used because the adorner accepts only one parameter;
Abstractmethod
Python's abstract class and Java are not the same, Java abstract class can not be instantiated, while the abstract method subclass must be implemented, otherwise error! However, the Python abstract class can be instantiated by default, or it can be said that if we define an abstract class: it cannot instantiate itself, subclasses must implement abstract methods, and the base classes we write generally are not abstract classes. What if you want to implement the effects of an abstract class in Java? Using the Abstractmethod adorner means an abstract method.
When any method in a class is Abstractmethod decorated, the class cannot be instantiated and subclasses must implement methods that are abstractmethod decorated.
from abc import abstractmethod,ABCMetaclass Animal(metaclass=ABCMeta): @abstractmethod def eat(self): passclass Person(Animal): _num_ear = 2 def eat(self): print('eat thing') @classmethod def go(cls): print(cls._num_ear) print('go')
If you need to define a class that is an abstract class, then it needs to inherit Abcmeta instead of object, so that the Abstractmethod adorner will work. It works by setting the __isabstractmethod__ property of a method to true so that the interpreter checks to see if the subclass implements an abstract method.