Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the decorator with Python

Shulou Source: shulou.com Published: 2022-06-02 18:58:48 09月11日 Update

Editor to share with you how to achieve Python decorator, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

General decorator:

Go straight to the code.

Def demo (func_test): def wrapper (): print ('first decorator defined') func_test () print ('decorator end') return wrapper # be careful not to add parentheses @ demodef func_test (): print ('Xiaobai!!')

Directly use the original method of custom decorator enhancement.

Decorator with parameters:

Def demo (func_test): def wrapper (name): print ('the first decorator defined') func_test (name) print ('Oh, you are% s'% name) print ('decorator end') return wrapper@demodef func_test (name): print ('Hello, my name is% s'%name)

Don't you feel something?

Then there is the decorator without custom parameters.

Def demo (func_test): def wrapper (* name,**kwargs): print ('the first decorator defined') func_test (* name,**kwargs) print ('Oh You are% s ah'% name [0]) print ('you are% s'% name [1]) print ('decorator end') return wrapper@demodef func_test (name,age): print ('Hello, my name is% s'%name) print (' this year {} '.format (age))

Isn't that a simple answer? Ha ha ha ha

Multiple decorations are used together.

Def deco01 (func): def wrapper (* args,**kwargs): print ('layer 1 decorator') func (* args,**kwargs) print ('layer 1 decorator end') return wrapperdef deco02 (func): def wrapper (* args,**kwargs): print ('this is layer 2 decorator') print ('layer 2 decorator end') func (* args * * kwargs) return wrapper@deco02@deco01def func: print ('hello, here is a func') print ("result is% d"% (a+b+c)) print (' name: {} '.format (name))

If you look at the execution order, you can see that the decorator is executed from top to bottom.

Class decorator:

When using decorators, we can't write all the decorators and methods in the same file, we always have to separate them. This is about a class decorator.

Here, I probably wrote an example of reading a file decorator.

Class Mydecorator (): def _ _ init__ (self,func): # is defined as a private property self.func = func # implements the _ _ call__ method, making the object callable Callable objects can use def _ call__ (self, * args, * * kwargs) as functions: print ('test class decorator') self.func (* args) catalogue = args [0] suffix = catalogue.split ('.') [2] try: if suffix = 'json': with open (* args,'r') Encoding='utf-8') as file_object: contents = json.load (file_object) print (contents) else: with open (* args,'r' Encoding='utf-8') as file_object: contents = file_object.read () print (contents) except Exception as a: print ('error reading file pull: {}' .format (a) @ Mydecoratordef name (name): print ('Please enter the corresponding file path:% s'%name)

The above is all the contents of the article "how to implement the decorator in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Files objects that is methods articles two layers content parameters rookies ordinary next not much code examples functions multiple most properties sense parentheses Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi Shulou Information OPPO Reno Apple