1

Day 10 动态参数&名称空间,局部全部.函数嵌套&global nonlocal关键字.


 

 



一、动态参数

#形参 有3种动态参数
#*args 动态参数,不定长参数
def func (*args):
print(args,type(args))
func(1,2,"alex",3,4,55,5,)
#输出结果 (1, 2, 'alex', 3, 4, 55, 5) <class 'tuple'>

def func(a,b,c,*args):
print(a)
print(b)
print(c)
print(args,type(args))
func(1,2,"alex")
'''
#输出结果
# 1
# 2
# alex
# () <class 'tuple'>
'''
def func(*args,a,b,c):
print(a)
print(b)
print(c)
print(args,type(args))
func(1,2,"alex")
#结果将会报错。TypeError: func() missing 3 required keyword-only arguments: 'a', 'b', and 'c'
'''
'''
a=1
def func(a,b,c,sex="男",*args):
print(a)
print(b)
print(c)
print(sex)
print(args,type(args))
func(1,2,"alex","wusir","ritian")
#运行结果:
# 1
# 2
# alex
# wusir
# ('ritian',) <class 'tuple'>
'''
'''
def func(a,b,c,*args,sex="男"):
print(a)
print(b)
print(c)
print(sex)
print(args,type(args))
func(1,2,"alex","wusir","ritian",sex="女")
#输出结果:
# 1
# 2
# alex
# 女
# # ('wusir', 'ritian') <class 'tuple'>

'''
'''
## *kwargs

def func(**kwargs):
print(kwargs)
func(a=1,b=4,c=3,d=2)
# 结果 :{'a': 1, 'b': 4, 'c': 3, 'd': 2},结果输出的为字典模式.

'''
'''
def func(a,b,c,*args,sex="男",**kwargs):
print(a)
print(b)
print(c)
print(sex)
print(args,type(args))
print(kwargs)
func(1,2,"alex","wusir","ritian",sex="女",name="taibai",age=21)
# 输出结果:
# 1
# 2
# alex
# 女
# ('wusir', 'ritian') <class 'tuple'>
# {'name': 'taibai', 'age': 21}
#特点:位置参数,*args,默认参数,**kwargs

'''
'''
def func(*args,**kwargs):
#万能参数
print(args)
print(kwargs)
func(1,2,3,45,name="alex",age=56)
# 结果 :
# (1, 2, 3, 45)
# {'name': 'alex', 'age': 56}

'''
'''
def my_sum(*args):
count = 0
for i in args:
count+=i
return count
print(my_sum(1,2,3,4,78))
print(sum([1,2,3]))
# 结果 :
# 88
# 6
'''

'''
#魔法运算:打散
def func(*args, **kwargs):
print(args)

l1 = [1,2,3]
l2 = [1,2,3,4,5,55,3,0]
func(*l2,*l1)

# 输出结果:(1, 2, 3, 4, 5, 55, 3, 0, 1, 2, 3)

'''
def func(*args,**kwargs):
print(args)
print(kwargs)
dic ={"name":"alex","age":12}
dic2 ={"name":"jin","age":12}
func(**{"name1":"alex","age1":12},**{"name2":"jin","age2":22},name3="wusir")
二、名称空间,局部全部

'''
名称空间:
全局名称空间,
局部名称空间,
内置名称空间.(input,print,sum len)
加载顺序:
内置名称空间-->全局名称空间-->局部名称空间(执行函数时)

取值顺序:
先从局部名称空间找--> 全局名称空间找--->内置名称空间
'''
'''
name = "wusir"
def func():
name="alex"
print(name)
func()
'''

"""
def len(x):
return x
print(len([1,2,3]))
# 输出结果:[1, 2, 3]
"""
# 作用域的角度:
# 全局作用域 全局名称空间,内置名称空间
# 局部作用域,局部名称空间,


def func():
name ="alex"
print(globals())
print(locals())
func()

print(globals())
print(locals())

# 输出结果:
# {'__name__': '__main__', '__doc__': '\n#形参 有3种动态参数\n#*args 动态参数,不定长参数\ndef func (*args):\n print(args,type(args))\nfunc(1,2,"alex",3,4,55,5,)\n#输出结果 (1, 2, \'alex\', 3, 4, 55, 5) <class \'tuple\'>\n\ndef func(a,b,c,*args):\n print(a)\n print(b)\n print(c)\n print(args,type(args))\nfunc(1,2,"alex")\n', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0205F950>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/parcharm/12/10/1.py', '__cached__': None, 'func': <function func at 0x02165E88>}
# {'name': 'alex'}
# {'__name__': '__main__', '__doc__': '\n#形参 有3种动态参数\n#*args 动态参数,不定长参数\ndef func (*args):\n print(args,type(args))\nfunc(1,2,"alex",3,4,55,5,)\n#输出结果 (1, 2, \'alex\', 3, 4, 55, 5) <class \'tuple\'>\n\ndef func(a,b,c,*args):\n print(a)\n print(b)\n print(c)\n print(args,type(args))\nfunc(1,2,"alex")\n', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0205F950>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/parcharm/12/10/1.py', '__cached__': None, 'func': <function func at 0x02165E88>}
# {'__name__': '__main__', '__doc__': '\n#形参 有3种动态参数\n#*args 动态参数,不定长参数\ndef func (*args):\n print(args,type(args))\nfunc(1,2,"alex",3,4,55,5,)\n#输出结果 (1, 2, \'alex\', 3, 4, 55, 5) <class \'tuple\'>\n\ndef func(a,b,c,*args):\n print(a)\n print(b)\n print(c)\n print(args,type(args))\nfunc(1,2,"alex")\n', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0205F950>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/parcharm/12/10/1.py', '__cached__': None, 'func': <function func at 0x02165E88>

三、函数的嵌套

def fun1():
print(111)

def fun2():
print(222)
fun1()
fun2()
print(111)
#输出结果为
# 222
# 111

def fun2():
print(222)
def fun3():
print(666)
print(444)
fun3()
print(888)
print(333)
fun2()
print(555)
# 输出结果为 :
# 111
# 333
# 222
# 444
# 666
# 888
# 555

四 global nonlocal 关键字

global 1.声明一个全局变量
    #引用全局变量,并改变
def func()
  

def func():
global a
a = 2
print(a)
func()
print(a)
# 输出结果
# 2
# 2

#可变的数据类型
l1 =[1,2,3]
def func():
l1.append(444)
func()
print(l1)
#输出结果
# [1, 2, 3, 444]
'''

#nonlocal
a = 1
def func():
nonlocal a
a = 2
print(a)
func()
'''

#父级函数
def func():
name ="wusir"
def inner():
nonlocal name
name = "taibai"
print(name)
inner()
print(name)
func()
#运行结果 :[1, 2, 3, 444]

def add_b():
b =42
def do_global():
b = 10
print(b)# 第一步 b=10
def dd_nonlocal():
nonlocal b
b=b+20
print(b)#第二部 b=30
dd_nonlocal()
print(b)#第三步 b=30
do_global()
print(b)#第四步:b=42
add_b()
结果 :

10
30
30
42

posted @ 2018-02-01 16:27  萌哥-爱学习  阅读(208)  评论(0编辑  收藏  举报