About the Python decorator understanding

Source: Internet
Author: User

After looking at a lot of decorator code explanation, ignorant of me, suddenly epiphany. Main references

http://pythonmap.iteye.com/blog/1682696

Http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html

In fact, these two references are not going to go on at all. I don't understand it at all. Torture time, experiment code, casually toss, unexpectedly let me epiphany. Catch the essence, and then look at the simple. Another problem is not solved, is the use of adorners. The main reference http://www.cnblogs.com/rollenholt/archive/2012/05/02/2479833.html did not understand down. Look back.

Decorator Essence: appearance, fake actor, real actor. Appearance must carry parameters to receive the dummy, the true and false actors must carry the same parameters.

A stage to find out to play "Chi Dou", actors to make up into Hank sister-in-law, then need a is ready to Hank appearance, another is to find actors. The essence of the adorner is this.

>>>defA (m):#This A is Hank's appearance....Print "Hello"    #confuse the enemy...defB (): #假设演员是 b ...return "A Qing Sao"# This is what the actors need to do in this play ...   Print " World"    #confuse the enemy...returnB#This b is the actor, put B into the appearance of a.... #The above defines what Hank's appearance and actors are going to do.>>> @a#That means Hank....defZhidou ():#The actor's name is Zhidou ()...Print "Diao De Yi"   #confuse the enemy...return "Sha Jia Bang"   #confuse the enemy... hello#confuse the enemyWorld#confuse the enemy>>> Zhidou ()#The curtain opened, actor Zhidou has made a good makeup on the stage, began to perform'A Qing Sao' #看到了吧, the output is only a prescribed action, the others are clouds. 


Analyze the above code, where "confuse the enemy" part of the Hank, the rest is the essence of appearance A and assumed actor B and finally starred in the real actor Zhidou these three parts.

Where appearance a must carry a parameter, and can only carry one parameter, this parameter is used to receive the last assumed actor B, under the mask of appearance A to plug into B.

And there's a real actor Zhidou, that's fine. Simplify the code. What the real actor is going to do is the content of B, and the rest of the place, whatever it is, is confusing the audience before it starts.

Really started, do one thing, is b in the provisions of the matter.

Def a (m): #阿庆嫂扮相

def b (): Pass #演员要做的事情

return b #把 B plug to Hank's appearance

@a #按照 a standard dress up instruction text

Def Zhidou (): Pass #真演员开始化妆

Zhidou () #真演员开演

The above example is actor B without parameters, then the real actor Zhidou also take parameters.

If b takes the parameters, then the Zhidou will have the same parameters.

That's the way to play.

However, no matter how the change, appearance a can only take one parameter, and only one, return value, also must be this pseudo-actor.

So what is the role of this decorator? At present it is possible to dress up real actors according to different appearance.

Like what

@a

@A

def Zhidou ():p

That is, there are several appearance dressed up as real actors.

Look at the actual application of the specific dress up. Here is just an understanding.

Here is the appearance of the parameter. The confusing part is not written, only the useful part is written. Can take several parameters, now set with only one parameter. You can also B (x, y) and so on.

Def a (m):

def b (x): Return x+x

Return b

@a

def Zhidou (y): return "Hello"

Zhidou (3) #运行结果就是一个6, did not output any "hello". Here is a confusing part of "hello", just to make it clearer that this part, no matter what it is written, will not work.

So, remember, the three great compositions of adorners, appearance, fake actors, real actors. The appearance must be taken with parameters to receive the false actor return. The true and false actors must wear the same parameters.

Still very happy. Remember three words when programming, appearance, fake actors, real actors . The rest is on the back. Understand the essence, you can look at others code, slowly learn.

Attach Hank appearance One, Rouse the drowsy afternoon spirit

About the Python decorator understanding

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.