Learning python ninth day - Functions

Yesterday learned some basic functions using the learn function in some special usage today.

 First, an arbitrary number of arguments passed

1, you can create topping the parameter * * Let python create an empty tuple called topping, and will receive ownership are packaged into this tuple.

 

 

 

2, any number of keyword arguments

Sometimes, it is necessary to accept any number of arguments, but I do not know in advance what information is passed to the function of Dehui City. In this case, the function may be written to accept any number of key-value pairs, usually with ** <dictionary name>

 

 

 

 

 Second, the decorator

  Decoration is a bit complicated, do not understand the sense of self, say, or others microblogging excerpt

Excerpt from https://blog.csdn.net/ljt735029684/article/details/80703649

I think in the beginning to learn one thing, it should read 80% of the base content with 20% of the time, the remaining 20% ​​of the content need to use 80% of the time to in-depth understanding of this article is to let you use 20% 80% of the time to read the contents of

Reference herein https://foofish.net/python-decorator.html

In this dynamic python language, everything is an object, including functions are objects, so there will be some closure

Because I experience shortness if something goes wrong please correct me big brother, thank you very much!

1. What is closure

 

 This function further comprises a containing function, and this function is embedded, using the parameters of the external function.

Above this function, it is referred to as a closure. Different from ordinary function is inline function p_name with variable external function name, the external variable life cycle is extended. The normal function of the variable, when entering other functions, variables can not be used again.

This internal function calls the behavior of external variables, is called closure.

Variable life cycle was extended because of closures using the python in a magical properties __closure__, which is responsible for the external variable life cycle extension.

You can understand in order to remember, anyway, name or indentation on print_func in, so internal functions can be taken for granted in p_name (some people just understood, though not quite)

Closures what's the use? Closure use big, we usually use a decorator, closure is achieved

If we do not pass a string as an argument, but it passed as an argument

 

 

 

Export

 

, The function passed as a parameter, the purpose is to add something before you run this function, because the function is passed set_string, the execution order is:

Execution order

 

print_func is not important, it is only used to pass the Senate, it is important p_name, it add content

If you need to use parameters:

Export

 

 

This feature is due to the python everywhere is the object, the function can also be used as an object. For example, in this example. foo with p_name is a reference point to the same function. If you do not understand the reference, then you put foo as the same thing with p_name, not bracketed as a function of the object itself, this call method in parentheses represents. That call foo foo () is equivalent to p_name (). The set_string with the same name is a reference to a function. This is why many people say is the cause of decorator syntax sugar.

2, decorator

Examples of the above fact, passed into the function, is a decorator, Python to use @ decorator, then the object name is a function, i.e. set_string ( 'Tory')

 

 You may think: is the first implementation set_string (string) of the head section, and then enter print_func, to p_name the contents of the executed, the last execution content set_string (string) indented portion

If you need to function in the example of the parameters used to, you can use * args and ** kwargs function accepts all the parameters, such as so write

Output

If you need to make the scene more decorative applications, you can use three nested, by passing parameters

Export

name still corresponds to the function object, because of our decorator in the incoming parameters, so inline function corresponds to the function object

 

3, rear decorative function
if you want to rear decorative function, the innermost layer can be, when you run the function, a coding function after the operation, (innermost layer can not write return, because it is a direct function with a function pointer operation, without distinction in front parentheses return, is a function of the body, the innermost one is parenthesized, is running instance)

 

 

 

4, decorators what's the use?

Specifically what use is it. It may be used to control the execution sequence embodied in general, together with any instructions to the function or the log.

Start log
function or operation code block
ends log

This practice is called AOP (Aspect Oriented Programming) Oriented Programming in Java. In simple terms as above. Need to add some decoration to the method (more correctly it should be said as a section is inserted, in line with pluggable thought). Decorated as an example, one day you want to give this back and forth a method using the log, so you can use a decorator. Then after a few days, you have to use this function, but this time you do not log, and you want to give him set up access control. The code for the function that you absolutely do not move, just write a decorated access control device. Then add on the line

Decorator great extent to reuse code, simply write the code of good helper

5, the execution order of a plurality of decorator

 

 


Execution order is equivalent to

 

 


----------------
Disclaimer: This article is CSDN bloggers 'true throughout Sh' original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/ljt735029684/article/details/80703649

Guess you like

Origin www.cnblogs.com/xiaobai-yang/p/11627667.html