Introduction to functions and function parameters of the python decorator

Source: Internet
Author: User
Tags python decorator

To put it simply, the decorator is mainly used to modify functions. It appears to define static methods when introducing Class methods and static methods. For example, to declare the Foo () function as a static function CopyCode The Code is as follows: Class myclass (object ):
Def staticfoo ():
............
............
Staticfoo = staticmethod (staticfoo)

You can use the decorator method to achieve this:Copy codeThe Code is as follows: Class myclass (object ):
@ Staticmethod
Def staticfoo ():
.........
.........

This example is obviously easy to understand.

Let's take the following example. This example also involves an important part, that is, the essential understanding of functions in Python.

Code:Copy codeThe Code is as follows: #-*-coding: UTF-8 -*-
From time import ctime
From time import sleep
Def ftfunc (func ):
Def timef ():
Print "[% s] % s () called" % (ctime (), func. _ name __)
Return func ()
Return timef

@ Ftfunc
Def Foo ():
Print 'hello'

If _ name _ = '_ main __':

Foo ()
Sleep (2)

For I in range (2 ):
Sleep (1)
Foo ()

Run this code. We can see that the terminal will output the following content in sequence:


Here, the ftfunc function is a self-defined function. This function is a function as a parameter, which meets the requirements of being a decorator, according to the above Equivalent Conversion rule for the decorator, this code

Copy code The Code is as follows: @ ftfunc
Def Foo ():
Print 'hello'

It can be converted into the following code:Copy codeThe Code is as follows: def Foo ():
Print 'hello'

Foo = ftfunc (FOO)

Combined with the original code above, we will soon be able to experience the role of the decorator.

However, when I was writing this code, an error occurred:

This code:Copy codeThe Code is as follows: Return func ()
Return timef

I wrote:Copy codeThe Code is as follows: Return func
Return timef

Therefore, the output results are different. Later, an important concept was found: "foo" is a reference to a function object, while "Foo ()" is a call to a function object. Object Reference is an important basic concept of Python. in Python, everything is an object, and the type is an object, not a variable. All variables are just object references, which is equivalent to directing this variable to this object. "Foo" can be understood as a variable, but it points to the object of a function. "Foo ()" is a function object call. To call this object, you must execute the function of this function. Here, you need to understand your taste. Based on this:

The running result of such a piece of code is exactly the same as that of the previous one. Note that comparing the differences with the code just now makes it easier to understand.Copy codeThe Code is as follows: #-*-coding: UTF-8 -*-
From time import ctime
From time import sleep
Def ftfunc (func ):
Def timef ():
Print "[% s] % s () called" % (ctime (), func. _ name __)
Return func
Return timef

@ Ftfunc
Def Foo ():
Print 'hello'

If _ name _ = '_ main __':

Foo ()()
Sleep (2)

For I in range (2 ):
Sleep (1)
Foo ()()

Running result of this Code:

In fact, you can also add parentheses to the returned timef function to see what the results will look like. You can better understand the concept of functions in Python.

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.