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 use the decorator correctly

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the decorator correctly". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "how to use the decorator correctly".

1. problem

The general question is, if you want to customize a Python decorator, can I write the decorator like this? If not, then why?

Import datetime import time def print_time (g): def f (): print ('start time') print (datetime.datetime.today ()) g () print ('end time') print (datetime.datetime.today ()) f ()

The following decorates the function foo with print_time:

@ print_time def foo (): time.sleep (2) print ('hello world')

When the foo function is called, the following exception is thrown:

Foo ()-TypeError Traceback (most recent call last) in-> 1 foo () TypeError: 'NoneType' object is not callable

Therefore, according to the above definition of print_time decorator, it certainly will not work.

two。 Why not

If you want to understand why not, you must first know the nature of the grammar of decorator. In fact, it is very simple, @ print_time decorate foo function is equal to:

Foo = print_time (foo)

This is the line of code, and there is nothing else.

Because the above print_time has no return value, the foo function becomes None after being assigned to the foo function, so 'NoneType' object is not callable' is thrown when foo () is called

That's not surprising.

3. How to write it?

Print_time needs to return a function, so after assigning a value to the foo function, the correct way to write it is as follows:

Import datetime import time def print_time (g): def f (): print ('start time') print (datetime.datetime.today ()) g () print ('end time') print (datetime.datetime.today ()) return f

Decorative foo:

@ print_time def foo (): time.sleep (2) print ('hello world')

Call foo, and the result is as follows:

Foo () start time 2021-04-02 22 32 purl 49.114124 hello world end time 2021-04-02 22 purl 32 purl 51.119506

Everything's fine.

4. Benefits of decorator

The above custom print_time decorator can decorate not only foo functions, but also any other functions and in-class methods.

Decorate any function foo2:

@ print_time def foo2 (): print ('this is foo2')

To decorate the in-class method foo3, you need to modify the original print_time slightly:

Def print_time (g): def f (* args, * * kargs): print ('start time') print (datetime.datetime.today ()) g (* args, * * kargs) print ('end time') print (datetime.datetime.today ()) return f

Add print_time decorations to the foo3 method in class MyClass:

Class MyClass (object): @ print_time def foo3 (self): print ('this is a method of class')

The implementation results are as follows:

MyClass (). Foo3 () start time 2021-04-02 23 this is a method of class 16buret 32.094025 this is a method of class end time 2021-04-02 23purl 16purl 32.094078

The above is the popular explanation of the decorator, which can be used more often, making our code more refined and readable.

Thank you for your reading, the above is the content of "how to use the decorator correctly". After the study of this article, I believe you have a deeper understanding of how to use the decorator correctly, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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