Decorator in Python

Source: Internet
Author: User
I remember that when I first learned Python several years ago, I thought that I could not understand the strange words in the 9 Yin Zhenjing, but it was indeed a very hard-to-understand concept, I believe that many beginners may also be confused. Therefore, this article mainly introduces the relevant information about the Python modifier. For more information, see. I remember that when I first learned Python several years ago, I thought that I could not understand the strange words in the 9 Yin Zhenjing, but it was indeed a very hard-to-understand concept, I believe that many beginners may also be confused. Therefore, this article mainly introduces the relevant information about the Python modifier. For more information, see.

This article will show you how sacred the ornament is. after reading this article, it will no longer be difficult.

1. what is a decoration device?

Someone commented on the decorator on the internet. I think it is interesting to write, and it is a metaphor.

  • Everybody's underpants are mainly used to hide their shame, but in the winter it won't be able to protect us against the cold and swollen wood?

  • One way we can think of is to transform the underwear to make it thicker and longer. in this way, it not only has the shame function, but also provides warmth, but there is a problem, after being transformed into a pair of trousers, this underwear is essentially no longer a real underwear, even though it has a shame function. So smart people invented trousers.

  • Without affecting your underwear, you can directly put your trousers on the outside of your underwear so that the underwear is still underwear.

The decorator is like the trousers we mentioned here. without affecting the effect of the underwear, it provides us with a warm effect.

2. four steps forward for the decoration device

Why do you need to put the decorator at the end of the entry? because it is too difficult, there are many prerequisites to understand it. I believe everyone understands Python functions, but do you know that functions are also an object that can be passed like parameters? let's look at the example below:

1) functions are also objects

def message(word='hello'): return word.upper()+'!'print message()>>HELLO!my_message=messageprint my_message>>
 
  print my_message()>>HELLO!
 

That is to say, the message can be assigned to another variable.

2) functions can be nested and defined inside another function.

def show(): print 'Run in show()' def message(word='hello'): return word print message() show()>>Run in show()hello

Messages can be nested in the show function. when you call show, the message function is run.

3) the function is returned as a parameter.

A function can also be used as the return value of another function.

>>
 
  hello...
 

4) input functions as parameters

Create a getName function and pass the function as a parameter to the foo function.

>>I will call the getName function laterleo

3. decorator

1). after the previous steps, you should understand that a function can be passed in as a parameter, or when the parameter is returned, it can also be nested.
The decorator is actually a re-packaging of the function. it can add the function without changing the function. it can execute a piece of code before or after the function is executed.

a_stand_alone_function()>>I am a stand alone function,don't you dare modify mea_stand_alone_function_decorated=my_new_decorator(a_stand_alone_function)a_stand_alone_function_decorated()>>Before the function runsI am a stand alone function,don't you dare modify meAfter the function runs

2). Use decorator

>>hello

One thing to note is that the order of the decorators has changed and the results are different.

@makeitalic@makebolddef word(): return "hello"print word()>>hello

Now let's get started with the decorator. if you have any questions, you can leave a message to discuss with me. In fact, there are still many advanced usages of the decorator, such as passing parameters from the decorator and decorating the class. we will explain them later.

Summary

The above is a detailed description of the decorator in Python. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.