Nested namespaces and scope day12 variable length parameter function object, functions

Vararg

 

Variable-length argument of *

 

def func (name, pwd, * args): # args receive extra position argument, using args a convention known as 
Print ( 'name:', name, 'pwd:', pwd)
Print (args)
return. 1

RES = func ( 'nick', 123658, 18, 180, 140, 'a', 234) # func function when the user would enter the function information of the user transmitted, but does not know several pass
print (res)


vararg of **
DEF FUNC (name, pwd, ** kwargs): # kwargs receive extra keyword arguments to the dictionary is stored, using the convention is commonly known as kwargs
    Print ( 'name:', name, 'pwd:', pwd) 
Print (kwargs)
return. 1

RES = FUNC ( 'Nick', 123 658, Age = 18 is, height = 180 [) # 'Age': 18 is
Print (RES)


The variable length parameter associated with the * and **

DEF FUNC (name, pwd, args *, ** kwargs):
Print ( 'name:', name, 'pwd:', pwd)
Print (args)
Print (kwargs)
return . 1


RES = FUNC ( 'Nick', 123 658,. 1, 2,. 3,. 4, Age = 18 is, height = 180 [)
Print (RES)


Learn
DEF FUNC (name, pwd, X, Y, Z):
Print ( 'name : ', name,' pwd: ', pwd)
Print (X, Y, Z)
return. 1


TUP = (. 4, 2,. 3)
RES = FUNC (' Nick ', 123 658, * TUP) # * will tuples play scattered into the position arguments passed parameter
# Print (RES)


DEF FUNC (name, pwd,** kwargs):
Print ( 'name:', name, 'pwd:', pwd)
Print (kwargs)
return. 1


DIC = { 'A':. 1, 'B': # ** 2} DIC ->. 1 = A, B = 2
RES = FUNC ( 'Nick', 123 658, DIC **) ** # will break as the keyword dictionary arguments passed parameter
# res = func ( 'nick' , 123658, a = 1, b = 2 ) ** # will break as the keyword dictionary parameter arguments passed to
print (res)


function object
DEF FUNC (): 
Print ( 'from FUNC')

to function as the object with

print (func) # func points to a memory address, the function name

A = 1
Print (A) # variable name


1) the variable name can be referenced, the function name can also be referenced

B = A # B =. 1

f = FUNC # FUNC can be added () call, then f may be added () call
Print (f, FUNC)
f () # FUNC ()

2) the variable name can be placed class data type into the container

lt = [. 1, 2, A, FUNC]
lt [-1] ()

. 3) can also be used as the parameter variable name

DEF F2 (name): FUNC # name =
name () FUNC # ()

# F2 (a)
F2 (FUNC)


. 4) the variable name as the return value of the function

DEF F3 (name): # name = FUNC
return name # name = FUNC

RES = F3 (FUNC) # RES = FUNC
Print (RES)
RES ()

summary : variable name identical to the function name, variable name conventional method i.e., the function name also has been cited; as container elements; as function parameters; as the function return value


Nested functions
def f1():
def f2():
print('from f2')

return f2

abc = f1() # f1()拿到函数的返回值,函数的返回值是f2, abc就相当于f2
abc()


# 定义一个circle函数,通过传参的形式得到园的 面积 或者 周长
import cmath

def circle(r, action):
if action == 'area':
return cmath.pi * r ** 2

elif action == 'zhouchang':
return 2 * cmath.pi * r


area = circle(3, 'area')
print(area)

zhouchang = circle(3, 'zhouchang')
print(zhouchang)


# 使用函数的嵌套定义
def circle(r, action):
def area():
return cmath.pi * r ** 2

def zhouchang():
return 2 * cmath.pi * r

if action == 'area':
area()

elif action == 'zhouchang':
zhouchang()

area = circle(3, 'area')
print(area)

zhouchang = circle(3, 'zhouchang')
print(zhouchang)


# 比较两个值
def self_max(x, y):
if x > y:
return x
return y


res = self_max(10, 20)
print(res)



# 比较四个值 函数的调用嵌套
def self_4_max(x, y, z, c):
res1 = self_max(x, y)
res2 = self_max(z, c)
res = self_max(res1, res2)

return res


res = self_4_max(10, 20, 1, 100)
print(res)


名称空间和作用域
名称空间: 存放名字(变量名/函数名)

内置名称空间: python解释器独有的
如:
len([1, 2, 3])
int('10')

函数调用必须得定义, 从来都没定义过. Python解释器启动的时候python自动开辟内置名称空间存放了这些python的内置方法,python解释器停止解释之后才会销毁


全局名称空间: 除了内置和局部,其他的都是全局


全局需要自己定义, python文件执行之后才可能有全局名称空间,文件结束之后才会销毁


局部名称空间: 函数内定义的变量名/函数名都存放在局部名称空间


局部也需要自己定义, 必须得在函数调用之后才会生成, 调用结束之后就会销毁


z = 10
def f1():
x = 10
def f2():
y = 10
print('from f2')

三种名称空间的执行顺序: 内置 --> 全局 --> 局部

x = 1
def f1():
x = 3

f1()

print(x)


# 三种名称空间的查找顺序: 首先从自己当前位置开始 --> 局部 --> 全局 --> 内置


作用域: 产生作用的范围

x = 1
def f1():
x = 3
print(x) #1

x=1 和 x=3 的x不是同一个东西
全局作用域: 全局+内置名称空间中的变量,全局作用域中的变量只能在全局中使用

局部作用域: 局部名称空间中的变量,局部作用域中的变量只能在局部中使用


x = 1
def f1():
print(x)
# f1中的局部

def f2():
x = 2 # x=2只能在f2中使用
f1()

f2() # 1, f1中的局部和f2中的局部互不干涉

作用域关系在函数定义阶段就已经确定死了


仅做了解

global

x = 1

def f1():
global x # 声明x为全局的x
x = 3
f1()
print(x) # 3


nonlocal

def f1():
x = 1
def f2():
nonlocal x # 针对嵌套函数局部之间的修改
x = 3
f2()
print(x)

f1() # 3



建议使用


lt = [1,2,3] # 作用域关系仅适用不可变数据类型,不适用于可变数据类型

def f1():
lt.append(4)

f1()

print(lt) #[1,2,3,4]



dic = dict()

def login():
dic['name'] = 'nick'
login()

def shopping():
if dic.get('name'):
print('购物成功')
else:
print('请登录')
shopping()


# s = '空'
#
# def login():
# s = 'nick'
# login()
# print(s)






Guess you like

Origin www.cnblogs.com/wwei4332/p/11329149.html