Optional parameter object re-used when instantiating multiple objects

Arnaud Delobelle arnodel at googlemail.com
Sat Nov 15 04:58:13 EST 2008


Rick Giuly <rgiuly.group at yahoo.com> writes:

> Hello All,

Hello,

> Why is python designed so that b and c (according to code below)
> actually share the same list object? It seems more natural to me that
> each object would be created with a new list object in the points
> variable.
>
> class Blob:
>     def __init__(self, points=[]):
>         self._points = points
>
>
> b = Blob()
> c = Blob()
>
> b._points.append(1)
> c._points.append(2)
>
> print b._points
>
> # this will show that b._points is the same object as c._points

This is probably the MFAQ (Most FAQ)!

Have a look in http://www.python.org/doc/faq/ (I can't point at the
question as my internet pipes to the US are very rusty this morning)

HTH

-- 
Arnaud



More information about the Python-list mailing list