Python beginners (4)
11. Functions
In Python, def is defined as a function. If a code segment is used in multiple places, define it as a function or implement a specific function, it can also be defined as a function;
A function func (), func is the function name, which is the function reference (pointing to the function address), and the brackets func () indicate that the function is executed;
The variables defined in the function are local variables and cannot be referenced in the function body (variables defined in the for loop can be referenced outside the for loop body );
The dynamic parameter func (* args1, ** args2) of the function. * args1 stores the passed parameters in args1 as tuples, ** args2 will include parameters passed in a specific form in args2 in the dictionary form, for example: func (a, B, c, name = 'job ', age = '17'), where a, B, and c are variables, name and age are keys, and the values following the equal signs are values;
Functions in the file will not be run. Python only loads it unless it is referenced for execution. A Python file has a system variable named "_ name _" and the default value is the current file name, however, the default value of the executed file is "_ main _". Therefore, you can use "if '_ name' =' _ main __': "As the File Execution portal;
If this function does not use return to specify the return value of this function, the return value of this function is None by default.
>>> Def func (* args1, ** args2): # dynamic parameters do not need to specify the number of parameters and the name of each parameter.
Print (args1)
Print (args2)
Print (args1 [2])
Print (args2 ['name'])
>>> Func
<Function func at 0x0000000003450598>
>>> Result = func (1, 2, 3, name = 'job', age = '17 ')
(1, 2, 3)
{'Age': '17', 'name': 'job '}
3
Jom
>>> Print (result) # The return value is not specified using return.
None
12. iterator and Generator
Iterator:
The iterator is used to access elements in the set. You do not need to prepare all elements to be iterated in advance by using the iterator. This element is calculated only when it is iterated to an element, this element does not exist before or after it. Due to the characteristics of the iterator, it traverses the memory occupied by the collection element and is suitable for accessing some extremely large or even infinite sets;
When an iterator iterates an element, it uses the _ next _ () method to constantly access the next element. It can only be "forward", not "backward ", you cannot access a specific element by subscript;
When the set is accessed, the iterator is "finished". to traverse the set, you need to create a new iterator.
Generator:
If an iterator is returned when a function is called, this function is called a generator. If a function contains the yield syntax, this function is also a generator;
Yield syntax features: When a function containing yield syntax is called, after yield is executed, the function is interrupted and the code after the function is executed continues, when this function is called again, it starts to execute the code after the yield function was last executed.
>>> Def generator (): n = 3 while n> 0: yield 'Hello python! '# Contains yield. This function is a generator that generates a string "hello python!" each time !" N-= 1 >>> iterator = generator () # returns an iterator >>> iterator. _ next _ () 'Hello python! '>>> Iterator. _ next _ ()' hello python! '>>> Iterator. _ next _ ()' hello python! '>>> Iterator. _ next _ () # "no" after access, this iterator cannot use Traceback (most recent call last ): file "<pyshell #13>", line 1, in <module> iterator. _ next _ () StopIteration>
13. decorator
The modifier is used to add additional functions for existing objects. It features that functions can be extended without changing the original code;
An object can be decorated by multiple decorators and can also be decorated with parameters (in the following example, @ decorator (func1, func2). Of course, the parameter is also the function name, hello will be used as the inner () parameter of the Function Represented by the return value of the decorator. It is no longer directly a decorator () parameter () the parameters are used by func1 and func2). Only the basic principle and process of the decorator are displayed.
Def decorator (func): print ('Hello python! ') Def inner (name): print ('hello, my friend') func (name) # func is the return inner @ decorator of the hello () function # The decorator is represented, function decorator () decoration function hello () def hello (name): print ('Hello % s! '% Name) hello ('job') # Run the function ---------------------------------------------------------------- hello python! Hello, my friendhello Jom!
When the decorator is loaded, that is, when it is loaded to the @ operator, a decorator (that is, the function modified by @) is run, and its return value replaces the address of the modified function, the address of the modified function is passed in the form of parameters of the decorator function. In the example: When loading to @ decorator, a decorator () function is run, and the function hello () calls the function name (that is, the function address hello) after the decorator () is executed, the function address of the inner () function is returned (this function address replaces the function address of the hello () function ), when you execute hello ('job'), the replaced function is executed, that is, the inner ('job'), and then the content in the inner () function is executed.