python ????????????????????????????????????????????????????????????global???nonlocal
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python ????????????????????????????????????????????????????????????global???nonlocal相关的知识,希望对你有一定的参考价值。
??????????????? ram arguments div UNC print ???????????? ????????? global
??????????????????
??????1.????????????????????????
??????def func(*args): *??????????????????
??????* ???????????????????????????????????????????????????????????????????????????
def fun(*args): print(args) fun(???aaa???,3,5,6,???ccc???) # ??????(???aaa???, 3, 5, 6, ???ccc???)
??????
??????2.????????????????????????
??????def func(**kwargs):
????????????????????????????????????
??????func(n=7,m="aa"):
??????**?????????????????????????????????????????????????????????????????????
def fun(**kwargs): print(kwargs) fun(name="Tom",age="???",hobby="???") # ?????? {???name???: ???Tom???, ???age???: ?????????, ???hobby???: ?????????}
????????????????????????????????? => *arg(arguments)(??????????????????) => ????????? => **kwargs(???????????????)
?????????????????????????????????????????????????????????
??????3.???????????????
??????def fun():
????????????"""
??????????????????????????????????????????????????????
?????????????????????
??????print(fun.__doc__) #document??????
def fun(a,b): """ ?????????????????????????????????????????? :param a: :param b: :return: """ return fun(5,6) print(fun.__doc__) #????????????????????????
??????
??????4.??????????????????
??????def fun(*args,**kwargs): #????????????
????????????pass
??????*???** ?????????????????????
??????*???** ?????????????????????
def fun(*args,**kwargs): print(args,kwargs) li = [???r???,???n???,???g???,???w???,???i???,???n???] dic = {???name???: ???uzi???,???age???:?????????,???hobby???:???adc???} fun(*li,**dic) # ?????? (???r???, ???n???, ???g???, ???w???, ???i???, ???n???) {???name???: ???uzi???, ???age???: ?????????, ???hobby???: ???adc???}
??????
??????????????????????????????
??????1.??????????????????
???????????????????????????????????????????????????
??????2.??????????????????
??????????????????????????????
?????? globals() ???????????????????????????????????????
??????locals() ?????????????????????????????????
a = 5 # ???????????? def fun(): a = 10 #???????????? print(locals()) print(globals()) fun()
??????
?????????????????????
????????????????????????????????????
a = 1 def fun(): a = 5 def inner(): a = 10 print(a) inner() print(a) print(a) fun()
??????
??????global
????????????1.???????????????????????????????????????????????????
????????????2.???????????????????????????
????????????(??????????????????????????????????????????????????????????????????????????????????????????????????????)
a = 1 def fun(): global a a += 5 print(a) # 6 print(a) # 1 fun() print(a) # 6
??????
??????nonlocal
????????????????????????????????????????????????????????????
a = 1 def fun(): a = 5 def inner(): nonlocal a a += 5 print(a) # 10 print(a) # 5 inner() print(a) # 10 print(a) # 1 fun() print(a) # 1
??????
??????
以上是关于python ????????????????????????????????????????????????????????????global???nonlocal的主要内容,如果未能解决你的问题,请参考以下文章