Basic Class/Instance Question

Mike akiany at gmail.com
Wed May 23 07:34:21 EDT 2007


Thanks Alan,

I am still perplexed why the default value of this object is shared.
hemm...d

Back to programming, :)
Sia


On May 23, 7:19 am, Alan Franzoni
<alan.franzoni_inva... at geemail.invalid> wrote:
> Il 23 May 2007 04:07:19 -0700, Siah ha scritto:
>
> > Ready to go insane here. Class A, taking on a default value for a
>
> __init__ is a function, taking a default value of [], which is a list,
> which is a mutable object. That said, you should remember that this means
> that such default value is 'shared' between all instances. If you don't
> want that, do something like:
>
> def __init__(self, defaultvalue=None):
>         if defaultvalue is None:
>                 defaultvalue=[]
>
> http://docs.python.org/tut/node6.html#SECTION006710000000000000000
>
> --
> Alan Franzoni <alan.franzoni.... at gmail.com>
> -
> Togli .xyz dalla mia email per contattarmi.
> Remove .xyz from my address in order to contact me.
> -
> GPG Key Fingerprint (Key ID = FE068F3E):
> 5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E





More information about the Python-list mailing list