r/Python • • Feb 28 '13

What's the one code snippet/python trick/etc did you wish you knew when you learned python?

I think this is cool:

import this

259 Upvotes

307 comments sorted by

View all comments

Show parent comments

7

u/keis Feb 28 '13

You already pointed out the difference. My local reference points the new list and only my local reference. The problem is when I care about the other references to the list and actually want to change the content.

2

u/jabbalaci Feb 28 '13

OK, I see. I thought you wanted to point on a new list. Here is an example of what you described:

>>> l = [1,2,3]
>>> g = l    # g refers to the same list as l
>>> g
[1, 2, 3]
>>> l[:] = [5,6,7]
>>> g
[5, 6, 7]