python syntax base (4) _ function

function

Def keyword

1. written format

Function name ():
function body

#例子如下
func():
 函数体

2. The function return value

Keyword return
return return the contents of a function call
return below the code is not executed, the function can be terminated, can not terminate the loop is terminated
return return multiple content when the form is Ganso
time return did not write return value is the return value is not written None None

3. Parameters

Parameter: it refers to a function definition in brackets parameter
arguments: calling the function in the parentheses are the arguments
passed parameters: the argument is a parameter passed to the procedure of
mixing parameters: key parameter and location parameter together
priority level
position parameter> keyword arguments

4. The ternary operator

= Variable conditions established condition determination condition results

def a(a,b):
    return a if a>b else b
print(a(1,2))

The dynamic parameters

* Args parameter is the universal position

def func(a,b,*args):
    print(a,b,args) #args传递的参数是元祖类型
func(1,2,3,45,['SkyRabbit'])
#结果如下
1 2 (3, 45, ['SkyRabbit'])
def func(a,b,**kwargs,):
    print(a,b,kwargs) #args传递的参数是字典类型
func(3,4,args=16,cs=5)
#结果如下
3 4 {'args': 16, 'cs': 5}

Location parameter> dynamic parameters> default parameters (key parameter)> Dynamic keyword arguments

Universal parameter passing: dynamic positional parameters, dynamic keyword arguments

6. Space

Examples of built-in space print () input ()
global space in the current py need to open up space to store files in the global space
local open space function space is local space

Loading sequence of
the inner space> Global Space> local space

Value order
partial space> Global Space> inner space

7. Scope

Global scope: the global space of the inner space +
the local scope: partial space

8. nested functions

No matter what position the sub-ah, as long as the function name + () function is to call this
function calls executed after the function body open space is automatically destroyed

9. modify variables

modify global keyword to declare global variables
nonlocal keyword in the outer function, modifying the value of the local variable space, space is not involved in the global space, modifying it only recently from the floor until you find the most outer function.

Guess you like

Origin www.cnblogs.com/SkyRabbit/p/11203715.html