I was sick with Python, so I tested it. For details, refer to the original article (https://qiita.com/yuta0801/items/f8690a6e129c594de5fb). (I just want to know the behavior, so I'm not interested in passing by reference. I'll study again next time.)
tmp.py
obj = {'arr': ['hoge']}
print(obj)
arr = obj['arr']
obj['arr'] = []
print(obj)
print(arr)
output
{'arr': ['hoge']}
{'arr': []}
['hoge']
tmp.py
obj = {'arr': ['hoge']}
print(obj)
arr = obj['arr']
arr.append('fuga')
print(obj)
print(arr)
obj['arr'] = []
print(obj)
print(arr)
output
{'arr': ['hoge']}
{'arr': ['hoge', 'fuga']}
['hoge', 'fuga']
{'arr': []}
['hoge', 'fuga']
Recommended Posts