Emperor's New Clothes (understanding and use of Python decorators)

Source: Internet
Author: User
Tags python decorator

Today, say the decorator.

In the Python language, the main function of adorners is to reduce the essence of repetitive input. With an adorner, you can use repetitive code very intuitively and elegantly, like a dress that can be worn to the emperor or worn by anyone, as long as you feel the need for this "dress"

Syntax: @ DECORATION function name as @yifu is an adorner, Yifu is a decorative function

All right, let's do a dress first. (Cosmetic function):

def Yifu (func): #yifu是装饰函数名, Func is the emperor (the Decorated function), which together is called: The Emperor's New Clothes

Def Gongneng ():

Print ("I am a decorative feature")

Func ()

Return Gongneng

The clothes are ready to add a lot of function (Gongneng), such as bow tie and other related accessories

Now, who's going to wear this shirt? The emperor or anyone else.

Let's build a group of people:

Def people1 ():

Print ("I am a passer-by")

Def people2 ():

Print ("I am a passer-by")

Def people3 ():

Print ("I am a passer-by")

Def people4 ():

Print ("I'm a passer-by")

Find 4 people, now have clothes, also someone, clothes to who wear, casually

For a passer-by, just put a piece of clothing in front of a pedestrian's definition of a function:

@yifu

Def people1 ():

Print ("I am a passer-by")

To other people as well.

The person who added the clothes will output the function of the clothes and the function of passers-by, if other people also want this dress, all right, then give a piece.

As an example, a decorator (clothing) that calculates the time of the function execution can be used in a function that you like to use, just like a dress casually worn.

#皇帝的新衣: Function Adorner import timedef Yongshi (func): #这件衣服的功能是计算函数执行的时间 def baozhuang (): #这个功能是计算函数执行时间 Start=Time.clock () func () #这是要装饰的函数 end=Time.clock () print ('perform adorner function-time:', end-start)returnbaozhuangdef foo1 (): Print ('A foo1 function that has not been decorated is called') @yongshi #这个就是做好的装饰器了def foo2 (): Print ('the Foo2 function that has been decorated is called') #调用原来的函数foo1 () print ('-'* -) Foo2 ()

Summary: Python decorator is not a special cow function, just for the simplicity and elegance of the code, in a number of repeated use of a function, you can consider using the adorner to more concise expression, of course, you can wear many pieces of clothing, many times decorate, His basic idea is that Python can define functions in the function, which is quite special, the adorner is based on this function and was born. Fully embodies the idea that some data are objects.

Do you have an impulse to use?

Next time want to say what did not think well, want to write again. Python primer or relatively sour, cultivation in their own!!!!

Emperor's New Clothes (understanding and use of Python decorators)

Related Article

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.