Alter list items within loop
usernet at ilthio.net
Thu Jun 11 17:12:07 EDT 2009
On 2009-06-11, Duncan Booth <duncan.booth at invalid.invalid> wrote:
> Tim Harig <usernet at ilthio.net> wrote:
>>> number 3 never gets printed. Does Python make a copy of a list before
>>> it iterates through it?:
>> No, complex types are passed by reference unless explicity copied.
> *All* types are passed by reference unless explicitly copied. Python does
> make special cases for simple and complex types.
That is technically true; but, you will not have this issue with simple
singlular data types. Technically the difference as to whether you will
have this problem depends on whether or not an object is mutable.
Simple objects (numbers and strings) are all immutable. Since this issue
revolves around changing objects in place, it cannot arise with immutable
objects. I am not always conscous of whether I am working with objects
that are mutable or immutable; but, I am generally concious of the general
complexity of the object. Whenever I am working with objects that are
complex, I am reminded to watch out for mutability issues. So, while it is not
totally correct to think of it this way, I find it an easier guideline to
More information about the Python-list