LoginSignup
0
0

More than 5 years have passed since last update.

Python > list > a[:] = [1, 2, 3] > identityを変更しない代入

Last updated at Posted at 2018-04-02
動作環境
ideone (Python 3.5)

@ Scipy Lecture notes, Edition 2017.1
p21 2.2.3 Assignment operator
@ Web版 1.2.2.3. Assignment operator
https://www.scipy-lectures.org/intro/language/basic_types.html#assignment-operator

In [6]: id(a)
Out[6]: 138641676
In [7]: a[:] = [1, 2, 3]  # Modifies object in place
In [9]: id(a)
Out[9]: 138641676  # Same as in Out[6], yours will differ...

上記のようなaへの代入だとidentityは変更されないとのこと。

ideoneでもidentityは変更されなかった。

a = [1, 2, 3]
print(id(a))

a = [4, 5, 6]
print(id(a))

a[:] = [7, 8, 9]
print(id(a))
run
47713454781832
47713454783560
47713454783560

identityを変更しないことによる利点については未消化。

0
0
0

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