Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply python decorator

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use python decorator". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use python decorator.

What is a decorator?

Literally, a decorator is a tool used to decorate other things. In python, decorators are divided into function decorators and class decorators.

In short, the function decorator is used to decorate the function, its main purpose is to increase the function of the objective function, the class decorator is the decorator of the decoration class, to increase the function of the class.

Function decorator

The decorator is essentially a nested function

Here is a simple decorator

# fun1 is the decorator name, and function refers to the decorated function def fun1 (function): def fun2 (): print ("here we go!") Function () # executes the decorated function or return function () return fun2

The outer fun1 () needs to pass in a parameter, which is used to pass in the function to be decorated, and the fun2 () inside needs to execute the parameter function.

This is passing the function as an argument to another function.

How to use it:

Add @ decorator name to the function that needs decoration

Fun1def fun3 (): print ("aaa") fun3 ()

It can be equivalent to this:

Def fun3 (): print ("aaa") fun = fun1 (fun3) fun ()

The output is consistent.

Execution result:

Here we go!

Aaa

If the passed-in function requires parameters, you can write like this:

Def fun1 (function): def fun2 (a): print ("here we go!") Function (a) return fun2@fun1def fun3 (a): print (a) fun3 ("aaa")

Output result:

Here we go!

Aaa

If you are not sure how many arguments are required for the passed-in function, you can write:

Def fun1 (function): def fun2 (* args, * * kwargs): print ("here we go!") Function (* args, * * kwargs) return fun2@fun1def fun3 (AMagneb): print (Arecedence b) @ fun1def fun4 (arecedicine bpenc): print (arecedence bpenc) fun3 ("aaa", "bbb") fun4 ("ccc", "ddd", "eee")

Running result:

Here we go!

Aaa bbb

Here we go!

Ccc ddd eee

If the function decorator needs to add parameters, you can write:

Def fun (msg): def fun1 (function): def fun2 (* args, * * kwargs): print ("here we go! {} ".format (msg) function (* args, * * kwargs) return fun2 return fun1@fun (msg=" children ") def fun3 (Arecom b): print (Arecom b) fun3 (" aaa "," bbb ")

Running result:

Here we go! Children

Aaa bbb

Class decorator

The use of a class decorator is basically the same as a function decorator, except that it is written in a class.

Class fun1 (object): def _ init__ (self, func): self.func = func def _ call__ (self, * args, * * kwargs): print (("here we go!") Self.func (* args, * * kwargs) @ fun1def fun2 (AMaginb): print (Amenb) fun2 ("aaa", "bbb")

Class decoration uses the _ _ call__ method of the class

Running result:

Here we go!

Aaa bbb

You can also write:

Class fun (object): def _ init__ (self, msg): self.msg = msg def _ call__ (self, func): def fun1 (* args, * * kwargs): print ("here we go! {0} ".format (self.msg) func (* args, * * kwargs) return fun1@fun (msg=" children ") def fun2 (a, b): print (a, b) fun2 (" aaa, "," bbb ")

Running result:

Here we go! Children

Aaa, bbb

At this point, I believe that everyone has a deeper understanding of "how to use python decorator", might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report