Bug in __init__?

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Tue Jan 22 04:30:59 EST 2008


Bart Ogryczak a écrit :
> On 2008-01-18, citizen Zbigniew Braniecki testified:
(snip usual default mutable list arg problem)
>> class A():
>>
>>    def add (self, el):
>>      self.lst.extend(el)
>>
>>    def __init__ (self, val=[]):
>>      print val
>>      self.lst = val
> 
> What you want probably is:
> 	def __init__ (self, val=None):
> 		if(val == None):

Better to use an identity test here - there's only one instance of the 
None object, and identity test is faster than equality test (one 
function call faster IIRC !-). Also, the parens are totallu useless.

                 if val is None:

> 			self.lst = []
> 		else:
> 			from copy import copy
> 			### see also deepcopy
> 			self.lst = copy(val)

What makes you think the OP wants a copy ?




More information about the Python-list mailing list