pythonシロ-ステップアップの道-day 9日--関数のネストとローカル変数の修正

4877 ワード

###     
'''



'''
def outer():
def inner():
print(" inner")
inner()
outer()
# inner()
'''
(1)
(2) ,
(3)
(4) ,



'''
# outer inner , inner smaller , smaller
# a = 17
def outer():
#a= 16
#id =99
def inner():
#a =15
def smaller():
#a =10
print(id)
print(" smaller")
smaller()
inner()
outer()
# LEGB ( )
'''
# LEGB ( )
B —— Builtin(Python);Python ( )( )
G —— Global(module); ( )( )
E —— Enclosing function locals; ( )( )
L —— Local(function); ( )( )
,
'''
#
'''
a, ,
, LEGB
'''
a= 10
def func():
a =20
del a
# print(a)
func()
# print(a)








































































































###     
'''



'''
def outer():
def inner():
print(" inner")
inner()
outer()
# inner()
'''
(1)
(2) ,
(3)
(4) ,



'''
# outer inner , inner smaller , smaller
# a = 17
def outer():
#a= 16
#id =99
def inner():
#a =15
def smaller():
#a =10
print(id)
print(" smaller")
smaller()
inner()
outer()
# LEGB ( )
'''
# LEGB ( )
B —— Builtin(Python);Python ( )( )
G —— Global(module); ( )( )
E —— Enclosing function locals; ( )( )
L —— Local(function); ( )( )
,
'''
#
'''
a, ,
, LEGB
'''
a= 10
def func():
a =20
del a
# print(a)
func()
# print(a)



### nonlocal       
'''
nonlocal
(1)
(2)
(3) ,
'''
#(1)nonlocal LEGB
def outer():
a =15
def inner():
nonlocal a
a= 17
print(a)
inner()
print(a)
outer()
#(2)nonlocal ,
a =16
def outer():
a =10
def inner():
#a =11
def smaller():
nonlocal a
a +=3
print(a)
smaller()
inner()
outer()
#(3) nonlocal ?
def outer():
#a = 3
lst =[1,2,3,4,5]
def smaller():
lst[2]+=5
smaller()
print(lst)
outer()








































































































































































転載先:https://www.cnblogs.com/-sch-593057927/p/10854235.html