0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonのオブジェクトでの代入と変更

Posted at

#概要
Pythonでモヤモヤしていたのでテスト。
詳しくは、元記事(https://qiita.com/yuta0801/items/f8690a6e129c594de5fb )を参照ください。
(私は挙動を知りたいだけなので、参照渡しがどうとか言われても興味がありません。また今度勉強します。)

#内容

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']

#すっきりした

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?