Bug in __init__?

Neil Cerutti mr.cerutti at gmail.com
Fri Jan 18 13:20:33 EST 2008


On Jan 18, 2008 12:33 PM, Zbigniew Braniecki
<zbigniew.braniecki at gmail.com> wrote:
> > class A:
> >    def __init__ (self, val=[]):
> >      print val
> >      self.lst = val
> >
> > val is created only *once* and shared across all instaces of A.
>
> Thanks for help guys!
>
> It's really a nice pitfall, I can hardly imagine anyone expecting this,
> or how easily could I find this info (e.g. what query should I give to
> google to get it without bothering people on this group)

In this unfortunate case, useful searches were "default arguments",
which would be hard to guess without already knowing what's going
wrong, or "python gotchas pitfalls", which is a good general purpose
search for when you can't understand what's happening in simple code.

-- 
Neil Cerutti <mr.cerutti+python at gmail.com>



More information about the Python-list mailing list