Python script:
def show(name):
def getName():
return _name
def setName(value):
_name = value
_name = ''
print('Input parameter: ', name)
print('Global variable: ', say_hello)
print('Change private variable: ', setName(name))
print('Get private variable: ', getName())
print('Private variable: ', _name)
print('Input parameter: ', name)
say_hello = 'hello'
show('Jim')
Output:
Input parameter: Jim
Global variable: hello Change
private variable: None
Get private variable:
Private variable:
Input parameter: Jim
Why doesn’t the inner function change the value of _name
, yet the function show
can get the value of say_hello
? I know it's a variable scope problem, but I want to know some detail.