Description of Python adorner function and function parameter usage

Source: Internet
Author: User
Simply put: The main function of the adorner is to make some modifications to the function, which appears in order to define static methods when introducing class methods and static methods. For example, to declare the Foo () function as a static function
Copy CodeThe code is as follows:


Class Myclass (object):
Def staticfoo ():
............
............
Staticfoo = Staticmethod (Staticfoo)


Can be achieved by means of an adorner:
Copy CodeThe code is as follows:


Class Myclass (object):
@staticmethod
Def staticfoo ():
.........
.........


This example is obviously easy to read.

In this case, let's take one of the following examples, which involves an important element in the understanding of the nature of the 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 in turn:


Where the Ftfunc function is our own custom function, this function is a function as a parameter function, which satisfies the requirement as an adorner, according to the above-mentioned equivalent transformation rules for adorners, this code
Copy the Code code as follows:


@ftfunc
def foo ():
print ' Hello '


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 appreciate the role of the adorner.

But when I was writing this code, there was a wrong place to call:

This piece of code:
Copy CodeThe code is as follows:


return func ()
Return TIMEF


Was written by me:
Copy CodeThe code is as follows:


return func
Return TIMEF


As a result, the output was different, and finally an important concept was found: "foo" is a reference to a function object, and "foo ()" is a call to a function object. About object references is an important basic concept of python, in Python Everything is an object, and the type belongs to an object, not a variable. All variables are simply references to objects, which are equivalent to pointing this variable to the object. "Foo" can be understood as a variable, but it is an object that points to a function. and "foo ()" is the invocation of the function object, which is the function of the function that calls this object. You need to understand taste slowly. Based on this:

The result of this piece of code is exactly the same as it was just now. Note the difference between the comparison and the code just now is more helpful 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 () ()


This code runs the result:

In fact, you can also add parentheses to the returned TIMEF function to see what the result will be. You can better understand the concept of functions in Python.

  • 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.