Alter list items within loop
Lie Ryan
lie.1296 at gmail.com
Mon Jun 15 04:00:11 EDT 2009
Tim Harig wrote:
> 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
> follow.
Everything is passed as an object[1], no matter how simple or complex,
mutable or immutable.
This can be seen:
>>> def foo(a):
... print id(a)
...
>>> b = 10
>>> id(b)
8578336
>>> foo(b)
8578336
[1] internally as PyObject pointer but that's implementation detail
More information about the Python-list
mailing list