what does 'a=b=c=[]' do

Ethan Furman ethan at stoneleaf.us
Fri Dec 23 03:38:04 EST 2011


Ian Kelly wrote:
> On Thu, Dec 22, 2011 at 7:10 PM, alex23 <wuwei23 at gmail.com> wrote:
>> On Dec 22, 6:51 pm, Rolf Camps <r... at roce.be> wrote:
>>> I'm afraid it's dangerous to encourage the use of '[]' as assignment to
>>> a parameter in a function definition. If you use the function several
>>> times 'default' always points to the same list.
 >>
>> I appreciate the concern, but adding a default argument guard would
>> not only obscure the code. It's irrelevant, as you recognise, because
>> no matter what, it's going to make copies of the default argument.
> 
> It's only irrelevant in the immediate context of the code you posted.
> But when Joe Novice sees your code and likes it and duplicates it a
> million times without receiving any warning about it, he's eventually
> going to write a function that modifies its default list argument, and
> he'll be in for a nasty surprise when he does.

And then he will learn about it and not make the mistake again (or if he 
does, it take much less time to figure it out).

~Ethan~



More information about the Python-list mailing list