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 define decorator in python

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

Share

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

This article mainly introduces "how to define decorators in python". In daily operation, I believe that many people have doubts about how to define decorators in python. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to define decorators in python". Next, please follow the editor to study!

Closure: a function f is defined within a function, and this function f refers to an external variable and returns the function f as a return value.

The above mentioned three conditions for closures:

A function f is defined in the 1 function.

2f function refers to external variables

3f is returned as a return value

Def T1 (): # definition T1 function xroom2 def f (): # T1 function internally defined f function print (x) # f function referencing a variable x return f # f that does not belong to itself is returned as a return value

And what is the ghost of the decorator? In fact, a kind of use of closure.

Decorator: add new functionality to the function without changing the code and calling method of the function (and others)!

So here comes the demand: for example, I've defined a download method, and I'm thinking of adding a function that requires login before it can be downloaded without changing the code and calling method of the download method.

Def get (file): # download method print ('download succeeded', file)

So how to achieve it? The closures mentioned above are used here.

Let's look at the closure, that is, a function f is defined within a function, and the function f refers to an external variable and returns the function f as a return value.

So we can try to do the first step of the above requirements, that is, do not change the code of the download method, and add a function that can only be downloaded after login.

Def get (file): # download method

Print ('download successfully', file) def auth (get,*args,**kwargs): print ('login method') get (* args,**kwargs) auth (get,'a.txt') # result: # login method # download a.txt successfully

But in this case, the invocation mode changes, so we can use closures:

Def get (file): # download method

Print ('download successful', file) def func (get): def auth (): print ('login method') get () return auth get=func (get) get ('dasd')

In this case, the parameters cannot be passed in, and the error parameters are not received:

Let's analyze that the above code func (get) should actually be the function name of auth, then get=func (get) assigns auth to the get variable, that is, the degree get=auth.

So we call get ('dasd') is auth (' dasd'), that is, we need to modify the auth function.

Def get (file): # download method

Print ('download successful', file) def func (get): def auth (file): print ('login method') get (file) return auth get=func (get) get ('dasd')

In this case, when get ('dasd') is called auth (' dasd'), then the login method is executed and get ('dasd') is executed. Here is the real download method.

To optimize, here's what it looks like:

Def auth (f): def wrapper (* args,**kwargs): print ('other functions or methods') f (* args,**kwargs) return wrapper

To decorate the get function in this way, you only need to get=auth (get), where the first get is the variable and the second get is the function name, so that the get does not change its code and calling mode, but adds function to it.

In python, get=auth (get) can be omitted as:

Def auth (f):

Def wrapper (* args,**kwargs): print ('login method') f (* args,**kwargs) return wrapper@auth # is equivalent to get=auth (get) def get (file): # download method print ('download successful', file)

Of course, above is the non-parameter decorator of the decorator, and there is also the parametric decorator:

# Parametric decorator def authx (a): def auth (f): def wrapper (* args, * * kwargs): if astata decoration: print ('login method') else: print ('adad')

F (* args, * * kwargs) return wrapper return auth@authx ('adas') # is equivalent to get=auth (get). Of course, the value of a has also been passed into def get (file): # download method print (' download successfully', file). The study on "how to define decorators in python" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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