[Tutor] default argument frustration

Brian van den Broek bvande at po-box.mcgill.ca
Fri Feb 11 09:04:27 CET 2005


Alan Gauld said unto the world upon 2005-02-10 02:58:

>        class Node:
>          def __init__(self,lines=[]):   # here's the zowie BvdB
>               self.lines = lines        
>          def append(self,item):         
>               self.lines.append(item)
>          def parse(self):
>               # your parsing method here.

Hi all,

I've been following the general approach that Alan suggested and have 
been happily making much headway. (The code is about twice as long as 
before and does about 4 times as many things :-) )

At first, I ended up with every single node being a copy of the first 
one processed. A bit of weeping later, I realized that this is from 
the feature [?] of Python that default arguments are evaluated just 
once. (Note the comment added above.)

Happily, I didn't need the default argument. And even if I did, 
there's the work around from the tutorial 
<http://docs.python.org/tut/node6.html#SECTION006720000000000000000> 
that's been under recent discussion in the
python's default argument value handling in functions - weird syntax? 
problem grappling with the concept
thread.

I may have missed the discussion in that thread but:

<venting>
FOR THE LOVE OF MIKE can someone tell me even one reason why this 
isn't a misfeature?!?!
</venting>

Sorry for the shouting, but this issue seems to come up a lot. 
Officially, I do know about it, but it often takes me considerable 
frustration to realize it's what's ruining my day. I'd be far happier 
if I could see that there was a good reason for this behaviour. 
(There'd even be a chance that I'd remember more readily--I hope so, 
these keyboards are getting expensive.) I'm sure there is one, as I 
don't believe that Dutchman are particularly given to sadism.

Thanks, and best to all,

Brian vdB



More information about the Tutor mailing list