[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Sticky Variables



Kris Schnee wrote:
I've run into that "sticky variables" problem again:

>>> code = {".":[],"c":["crate"]}
>>> a = code["."]
>>> b = code["."]
>>> a ==b
True
>>> a is b
True

One solution is:
>>> import copy
>>> a = copy.copy(code["."])
>>> b = copy.copy(code["."])
>>> a == b
True
>>> a is b
False

But is there a better way to make it clear I want the value, not an actual reference?


The reference to the list is what is intended to be returned -- how else would you modify such a list?

If you intend to take the list and modify it's contents locally without changing the original, then you should explicitly copy the original. If you're not intending to modify it locally, then you should use the reference.

-Jasper