Python adorner function and function parameter usage introduction _python

Source: Internet
Author: User
Tags in python
Simply put: The main role of the adorner is to modify the function, it appears in the introduction of class methods and static methods in order to define static methods appear. For example, to declare the Foo () function as a static function
Copy Code code as follows:

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

You can do this by using an adorner:
Copy Code code as follows:

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

This example is clearly easy to read.

So here's an example of this, which involves an important element in the understanding of the nature of the functions in Python.

Code:
Copy Code code 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 a function of our own custom, a function that takes a function as a parameter, which satisfies the requirement of being an adorner, according to our equivalent transformation rules for the adorner, this code

Copy Code code as follows:

@ftfunc
def foo ():
print ' Hello '

You can convert to the following code:
Copy Code code as follows:

def foo ():
print ' Hello '

foo = Ftfunc (foo)

Together with the original code above we will soon be able to understand the role of the adorner.

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

This piece of code:
Copy Code code as follows:

return func ()
Return TIMEF

Written by me:
Copy Code code as follows:

return func
Return TIMEF

So the output is not the same, and then finally found an important concept: "foo" is a reference to the function object, and "foo ()" is a function object call. About object references are an important basic concept of python, in Python Everything is an object, and the type belongs to the object, not to the variable. All variables are just references to objects, which is equivalent to having this variable point to this object. "Foo" can be understood to be just a variable, except that it points to a function object. and "foo ()" is called by the function object, that is, to invoke the object to perform this function. There is a need to understand taste slowly. Based on this:

Such a piece of code to run the result is exactly the same as just now. Pay attention to compare with just that section of the code differences, more conducive to understanding.
Copy Code code 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 results:

In fact, you can also add parentheses to the returned TIMEF function to see what the result will look like. 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.