Xi. functions
In Python, functions are defined in DEF to implement a function, and if a piece of code is used in multiple places, it may be defined as a function, or it can be used to implement a particular function, or it may be defined as a function;
A function func (), Func is the function name, is the function reference (the address of the function), and the parentheses func () means that the function is executed;
The variable defined in the function is a local variable, and in the function body it is not referenced (the variable defined in the For loop, which can be referenced outside the body of the For loop);
The dynamic parameter func (*ARGS1, **ARGS2) of the function, *ARGS1 will be passed in as a tuple of parameters in the form of ARGS1, **ARGS2 will be passed in a specific form of parameters in the form of a dictionary args2, for example: Func (A, B, C, name = ' Jom ', age = ' 17 '), where a, B, c are variables, name and age are key, and after the equals sign as value;
In the file the function is not run, Python just goes to load it, unless it is referenced to execute, Python file has a system variable "__name__", the default value is the current file name, but the default value of the executed file is "__main__", so you can use "if" __name ' = = ' __main__ ': "As the execution entrance of the document;
If this function does not use return to specify the return value of the function, the return value of the function defaults to none.
>>> def func (*ARGS1, **ARGS2): # dynamic parameter refers to parameter names without specifying parameters and parameters
Print (ARGS1)
Print (ARGS2)
Print (args1[2])
Print (args2[' name '])
>>> func
<function func at 0x0000000003450598>
>>> result = Func (1, 2, 3, name = ' Jom ', age = ' 17 ')
(1, 2, 3)
{' Age ': ' + ', ' name ': ' Jom '}
3
Jom
>>> print (Result) # no return value specified using return
None
12. Iterators and Generators
Iterators:
Iterators are used to access the elements in the collection, and the iterator does not need to be prepared in advance for all the elements that are to be iterated, only when the element is iterated to an element, which is not before or after the element, because the iterator, which iterates over the collection element, consumes little memory, For access to some particularly large or even infinite collections;
Iterators iterate over elements using the __next__ () method to continually access the next element, only "forward", not "back", or by subscript to access a particular element;
When the collection is finished, the iterator is "over", and if you want to traverse the collection again, you will need to create a new iterator.
Generator:
If an iterator is returned when a function is called, the function is a generator, and if a function contains yield syntax, the function is also a generator;
Yield syntax features: When a function containing yield syntax is called, after yield is executed, the function is interrupted, the code after the function is executed, and the next time the function is called, it is executed from the code after the last yield of the function.
>>>defGenerator (): N= 3 whilen >0:yield 'Hello python!' #containing yield, this function is the generator, each time a string "Hello python!" is generatedN-= 1 >>> iterator =Generator () # returns an iterator >>> iterator.__next__()'Hello python!'>>> iterator.__next__()'Hello python!'>>> iterator.__next__()'Hello python!'>>> iterator.__next__() # After the visit, "No", this iterator can no longer use the Traceback (most recent call last): File"<pyshell#13>", Line 1,inch<module>iterator.__next__() stopiteration>>>
13, the decorative device
The role of adorners is to add additional functionality to existing objects, characterized by the ability to expand without changing the original code;
An object can be decorated with multiple adorners, the adorner can also be decorated with parameters (in the following example can be @decorator (func1, Func2), of course, the parameter is also the function name, and Hello will be used as the adorner return value represented by the function (inner ()) parameters, No longer directly the parameters of the decorator () (decorator () parameters have been taken up by Func1 and Func2), this shows only the adorner fundamentals and flow.
defDecorator (func):Print('Hello python!') defInner (name):Print('Hello, my friend'func (name) # func is the Hello () function returninner@decorator # Adorner @, function decorator () decorative function hello () defHello (name):Print('Hello%s!'%name) Hello ('Jom') # Execute function --------------------------------------------------------------Hello Python!hello, my Friendhello jom!
When the adorner is loaded, that is, when loaded to the @ character, the adorner is run (that is, the function with the @ modifier), and its return value overrides the modified function address, and the address of the decorated function is passed into the adorner as an adorner function parameter. Example: When loading to @decorator, a decorator () function is run, when the function hello () functions as the adorner parameter func (called function address Hello), and decorator () returns the function inner () after execution. function address (which replaces the function address of function hello (), executes hello (' Jom '), executes the replacement function, i.e. inner (' Jom '), then executes the contents of the inner () function.
Beginner Python (iv)