[issue35176] for loop range bug

Dan Armentrout report at bugs.python.org
Tue Nov 6 09:23:25 EST 2018


Dan Armentrout <dla0009 at uah.edu> added the comment:

Thank you for your quick explanation of this to me.

Dan

On Tue, Nov 6, 2018 at 8:04 AM Steven D'Aprano <report at bugs.python.org>
wrote:

>
> Steven D'Aprano <steve+python at pearwood.info> added the comment:
>
> This is not a bug, this is standard behaviour, working as designed.
>
> 'a' is not a copy of the list 'x', 'a' is another name for the same list
> as 'x'. Any in-place modifications you make to 'a' happens to the object
> itself, the list, which is visible regardless of which name you refer to it
> by.
>
> If you are a C programmer, you can think of this as being similar to
> pointers: think of 'x' as a pointer to the list, and 'a' as a pointer to
> the same list. (That's more or less what happens under the hood.) From the
> Python level, we say that both names 'a' and 'x' refer to the same object.
>
> If you want a copy, you can use the copy module, or for lists, you can
> take a slice: a = x[:] makes a copy of the list.
>
> For immutable objects like strings, you don't need a copy, because you
> cannot modify them in place: any operation on a string always creates a new
> string, leaving the old one untouched.
>
> ----------
> nosy: +steven.daprano
> resolution:  -> not a bug
> stage:  -> resolved
> status: open -> closed
>
> _______________________________________
> Python tracker <report at bugs.python.org>
> <https://bugs.python.org/issue35176>
> _______________________________________
>

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35176>
_______________________________________


More information about the Python-bugs-list mailing list