Empty list as default parameter

anton muhin antonmuhin.REMOVE.ME.FOR.REAL.MAIL at rambler.ru
Fri Nov 21 09:15:19 EST 2003


Alex Panayotopoulos wrote:

> On Fri, 21 Nov 2003, anton muhin wrote:
> 
> 
> Was there any reason that this sort of behaviour was not implemented?
> 
> 
It was discussed several times (search for it, if you're interested), 
but I don't remeber details.

>>Mutable defaults are better avoided (except for some variants of memo
>>pattern). Standard trick is:
>>
>>    def __init__(self, myList = None):
>>        if myList is None:
>>            self.myList = []
>>        else:
>>            self.myList = myList
> 
> 
> Thank you. I shall use this in my code. (Although I would have preferred a 
> trick that uses less lines!)
> 

if you wish a one-linear, somehting like this might work:
self.myList = myList or []

regards,
anton.





More information about the Python-list mailing list