Functions-Objects, Nesting, Namespaces and Scope

The
most complete way to define a function : positional parameter default parameter* named keyword parameter**

Supplementary knowledge points: type hinting #A mechanism for adding annotations

def register(name,age):
def register(name:str,age:int)->str
print(name,age)
return 'ok'
register 登记注册

Closure function:
(1) Function object (a function can be used as a variable)
In python, a function is a first-class object and a first-class citizen

def func(): # func=memory address (any name points to a memory address)
pass
func() #function name plus () can trigger the operation of the function body code
func #represents the memory address of the function

1. Variables can be assigned
The memory address of the function can be assigned the memory address of the
number
def func(): # func = print('from func')
#x = 10 # The memory address of x = 10
#y=x # Variable name Can be assigned
#print(id(x),id(y)) #After assignment, it points to the same memory address as the original variable
f=func
#print(f)
print(f is func)

2. The memory address of a function can be passed as a parameter to another function

3. The memory address of the function can be used as the return value of the function
#def foo(f):
#return f
#res = foo(func)
#print(res)
#res()

4. The memory address of a function can be used as an element of the container type
#l = [x,func]
#print(l[0])
#print(l)
#l[1]()

(2) Function nesting
1. Function definition nesting
#def f1():
#x = 111
#def f2():
#print('from f2')
#print(x)
#print(f2)
#f2( )
#f1()
The variables and functions defined in the function can only be used internally (there is a closed effect)

2. Nested function call
def max2(x,y):
if x> y:
return x
else:
return y

def max4(a,b,c,d):
res1 = max2(a,b,)
res2 = max2(res1,c)
res3 = max2(res2,d)
return The
purpose of res3 : you can disassemble a large function into countless The small function of, the realization of the small function may only need a small amount of code, and then the small function can be called in turn in the large function (a programming routine)

(3) The name space
where the name and its corresponding memory address are stored is
divided into three categories. The
built-in name space
stores the name that comes with the python interpreter
print(len) #build in
print(input) #build in
life cycle: interpreter Generated when started, destroyed when the interpreter is closed


As long as the global name space defined at the top level is called the global
py program, it will be generated immediately when the
py program ends, and the global name space will end
#if True:
#y = 2
#def foo():
#z = 3
Local name space
storage Is the
life cycle of the name in the function : it is generated when the function is called, and destroyed when the function is finished

The priority of existing access is
from the current priority up to
L (in the belly) E (outer layer function) GB
Summary
1. Name access priority:
L->E->G->B
based on your current location layer by layer Look out
2. The nesting relationship of the namespace is generated during the function definition phase and when scanning the grammar, and has nothing to do with the call location

(4) Scope
1. Global scope: built-in name space, global name space (global survival, global effective)
2. Local scope: local name space (temporary survival)

globals() #Used to locally modify the immutable type variable in the global
case one
dic={"username":None} #The defined variable is a variable type
def log():
dic=123 #Local definition has no effect on the global
dic ['username']='egon'
log()
print(dic)
Case two
x=100
def log():
global x #Declare that the x variable is a global variable
x=200
log()
print(x)

nonlocal() #Used to locally change the immutable type variables in the outer level
def f1():
x = 100
def f2():
nonlocal x
x = 200
f2()
print(x)
f1()

Guess you like

Origin blog.51cto.com/15129993/2676367