[Tutor] The trap of the year

Karim karim.liateni at free.fr
Wed Jan 26 02:21:21 CET 2011


Sorry Alan,

When I read your tutorial I was probably blind... :-D
I am going to re-read it.

Regards
Karim

On 01/26/2011 01:30 AM, Alan Gauld wrote:
>
> "Karim" <karim.liateni at free.fr> wrote
>
>> If I understand a little bit what happen in:
>>
>> def __init__(self, parameters=[]):
>>    [...]
>>
>> The list argument is built before instance creation and indeed
>> constructor execution.
>> So this is the same list instance of constructor parameter for all new
>> instance creation.
>
> Yes that is a feature of Pythons default argument mechanism.
> It has been discussed many times on this list and in the docs.
> As you discovered the best way to avoid it is use None as the
> default then create a new list inside the function if it is None.
>
>> I never see warning about this fact in any books before.
>
> Its quite commonly warned against but until you get bitten
> the significance just passes you by! I bet if you look out for
> it now you'll see it often ebnough!
>
> HTH,
>
>



More information about the Tutor mailing list