Unexpected behavior when initializing class

Mel mwilson at the-wire.com
Wed Nov 28 16:04:39 CET 2007


Paul Rudin wrote:
> "alfred.fazio at gmail.com" <alfred.fazio at gmail.com> 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 you get the idea.)

Do test explicitly for None.  Otherwise, if you do

a = []
x = ThatClass (a)


it will so happen that x.values will be an empty list, but it won't be 
the same list as a.

	Mel.



More information about the Python-list mailing list