Re: Unexpected behavior when initializing class

2007-11-28 Thread Arnaud Delobelle
On Nov 28, 3:04 pm, Mel <[EMAIL PROTECTED]> wrote: > Paul Rudin wrote: > > "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > > A common paradigm to get round this - assuming you want a different > > empty list each time - is something like: > > > def __init__(self, v = None): > > self.values =

Re: Unexpected behavior when initializing class

2007-11-28 Thread Mel
Paul Rudin wrote: > "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > A common paradigm to get round this - assuming you want a different > empty list each time - is something like: > > def __init__(self, v = None): > self.values = v if v else [] > > (or maybe test explicitly for None, but y

Re: Unexpected behavior when initializing class

2007-11-28 Thread Duncan Booth
Paul Rudin <[EMAIL PROTECTED]> wrote: > You have to understand that the default value for v - an empty list - > is made at compile time Not at compile time: the default value is created at runtime when the def statement is executed. -- http://mail.python.org/mailman/listinfo/python-list

Re: Unexpected behavior when initializing class

2007-11-28 Thread John Machin
On Nov 28, 7:19 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Hello everybody, > > I've banged my ahead around for a while trying to figure out why > multiple instances of a class share the same instance variable. I've > stripped down my code to the following, which reproduces my proble

Re: Unexpected behavior when initializing class

2007-11-28 Thread [EMAIL PROTECTED]
On Nov 28, 3:31 am, Paul Rudin <[EMAIL PROTECTED]> wrote: > You have to understand that the default value for v - an empty list - > is made at compile time - and it's the *same* list every time it's > used i.e. if you don't pass in a value for v when you make new > instances of your class. *smack*

Re: Unexpected behavior when initializing class

2007-11-28 Thread Gary Herron
[EMAIL PROTECTED] wrote: > Hello everybody, > > I've banged my ahead around for a while trying to figure out why > multiple instances of a class share the same instance variable. I've > stripped down my code to the following, which reproduces my problem. > This is a *feature* of Python tha

Re: Unexpected behavior when initializing class

2007-11-28 Thread Paul Rudin
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > Hello everybody, > > I've banged my ahead around for a while trying to figure out why > multiple instances of a class share the same instance variable. I've > stripped down my code to the following, which reproduces my problem. > > class Test(