[Tutor] default argument frustration
Brian van den Broek
bvande at po-box.mcgill.ca
Sat Feb 12 23:03:42 CET 2005
I've combined a few email's worth of quoting as no previous post had
all the elements I wanted to refer to.
Alan Gauld said unto the world upon 2005-02-11 13:30:
>><venting>
>>FOR THE LOVE OF MIKE can someone tell me even one reason why this
>>isn't a misfeature?!?!
>></venting>
>
> Its the only sane way to implement default arguments. The whole
> point of function definitions is that they provide a single concise
> interface. The function should return the same result each time
> you call it with the same input. The only way to achieve that
> is to have the default calculated once.
Alan Gauld said unto the world upon 2005-02-12 03:41:
> Karl Pflästerer said unto the world upon 2005-02-11 16:44:
>>
>>IBTD.
>>With full lexical scope you only need to calculate the default
>>argument in the lexical scope it was defined in. Look at a
>>languale like Common Lisp.
>
>
> Sorry, you are quite right. I meant its the only sane way to do it
> given the way Python works.
>
> Lisp and others implement closures and defered evaluation quite
> nicely, but I don't think it would be easy to build that into Python.
> Although it might be easier now with nested functions etc...
>
> Alan G.
I get that Karl was saying that other languages (eg Lisp) can do it
differently, though the details of the Lisp comparison are lost on me.
Alan was saying that there is no other obvious way for Python to do it.
What I am still not clear on it is why Alan's claim is true. (Not
doubting it is, but would like to get why it is.)
Is this the rough idea this?:
The def statement is executed only once, and thus the default argument
is calculated just the once, too (on building of the function object).
So, any way around this feature of Python would either require that
defs be executed each time a function is called (giant performance hit
across the board even if doable) or require the addition of a new
reserved word, say redef, for function defs that should be
re-evaluated each time the function is run (again, if doable).
If that is in the vicinity of being right, I start to be less inclined
to vent. Python's conservatism abut adding new reserved words seems a
good thing, and enough to kill such a redef idea, if the performance
issue wasn't. That makes the workaround a la the Tutorial seem less
irksome.
Thanks and best to all,
Brian vdB
More information about the Tutor
mailing list