###理解列表的可变性###
#示例一 :为字符串追加内容——————类似值传递(pass—by—value)
print('类似值传递')
def add_str(in_func_obj):
print(f'In add [before] : in_func_obj="{in_func_obj}"')
in_func_obj += 'suffix'
print(f'In add [after] : in_func_obj="{in_func_obj}"')
orig_obj='foo'
print(f'Outside [before]: orig_obj="{orig_obj}"')
add_str(orig_obj)
print(f'Outside [after] : orig_obj="{orig_obj}"')
"""
输出
Outside [before]: orig_obj="foo"
In add [before] : in_func_obj="foo"
In add [after] : in_func_obj="foosuffix"
Outside [after] : orig_obj="foo"
"""
print('*************************************************')
#示例二:为列表追加内容(与上面的代码逻辑一致,但是吧字符串换成列表)——————引用传递(pass—by—reference)
print('类似引用传递')
def add_str(in_func_obj):
print(f'In add [before] : in_func_obj="{in_func_obj}"')
in_func_obj += ['baz']
print(f'In add [after] : in_func_obj="{in_func_obj}"')
orig_obj=['foo','bar']
print(f'Outside [before]: orig_obj="{orig_obj}"')
add_str(orig_obj)
print(f'Outside [after] : orig_obj="{orig_obj}"')
"""
输出
Outside [before]: orig_obj="['foo', 'bar']"
In add [before] : in_func_obj="['foo', 'bar']"
In add [after] : in_func_obj="['foo', 'bar', 'baz']"
Outside [after] : orig_obj="['foo', 'bar', 'baz']"
"""
"""
********************************************************************************************************************
python 在进行函数调用传参时,采用的既不是值传递,也不是引用传递,而是传递了‘变量所指对象的引用’(pass-by-ogbjedt-reference)
所以,当我们在函数内部执行in_func_obj += ...等修改操作时,是否会影响外部变量,只取决于in_func_obj所指向的的对象是否可变
********************************************************************************************************************
"""