python notes 4

Parameter
1 The default parameter is only assigned in the assignment phase, and subsequent changes will not affect
2 Variable-length parameter
1 Accepts the overflow positional argument *, the tuple is stored in the form and then assigned to the variable name after *
1 In the positional argument use

2
Use def foo(x,y,
z) in the arguments:
print(x,y)
print(z)
foo (1,2,*(12,34,5,6,7)) #return (12, 34, 5, 6, 7)
foo (1,2,(12,34,5,6,7)) #return ((12, 34, 5, 6, 7),)
2 accepts dict and gives positional arguments , and copy it to the variable name kwargs
def test_kwargs(x,y,
z):
print(z)
test_kwargs(1,s=3,d=4,k=5,y=2)
s = 3,d=4,k =5 is received by kwargs Argument
assignment
3 def test_kwargs(x,y, z):
print(z)
test_kwargs(1,
{'s':3,'d':4,'k':5,'y': 2})
At this time, z only accepts sdk but not y

function nesting
def t():
def t2():
return

namespaces namespace
stores the binding relationship between names and values

Classification of namespaces Built - in
namespace py 's
own name len max sum created
Destruction
closed and destroyed




namespace targeting
len = 10
def f1()
len=111
def f2()
f2()

len (1000)
f1 ()

Namespace is defined at definition time

scope scope = scope

the name of the built-in namespace in the global scope in the global namespace

Features Globally effective, global survival

local scope local namespace

locals local
globals global

def f1():
x = 10
def f2():
nonlocal x ###Declare x to be a variable in the outer layer of the current function Emphasize
x = 11
f2()
print(x)

f1() ##x = 11

x=111
def f1():
x = 10
def f2():
global x ##Find global variables

    x = 11
f2()
print(x)

f1 ()

1. Scope is scope

    - 全局范围(内置名称空间与全局名称空间属于该范围):全局存活,全局有效

  - local scope (to which the local namespace belongs): live temporarily, valid locally

2. The scope relationship is fixed in the function definition stage and has nothing to do with the calling location of the function, as follows

x=1
def f1():
def f2():
print(x)
return f2
x=100
def f3(func):
x=2
func()
x=10000
f3(f1())

3. View the scope: globals(), locals()

LEGB stands for name lookup order: locals -> enclosing function -> globals -> builtins
locals is the namespace inside the function, including local variables and parameters
enclosing the namespace of the outer nested function (common in closures)
globals global variables, functions Define the namespace of the module in which
builtins is the namespace of the built-in module

Application of Closure Function

def outter():
x = 1
def func():

    print(x)
return func

f = outter()

def func_f():
x = 111
print("func_f")
f()

func_f() ##Result x = 1

decorator
import time

def timmer(func):
def wrapper(args,kwargs):
start_time = time.time()
res = func(*args,**kwargs)
stop_time = time.time()
print("run time is {}".format(stop_time-start_time))
return res
return wrapper

@timmer

def foo():
time.sleep(3)
print("from foo")

timmer(foo())

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324623181&siteId=291194637