Pre-PEP: Dynamically evaluating default function arguments

John Roth newsgroups at jhrothjr.com
Tue Jan 6 19:54:59 EST 2004


"Daniel Ehrenberg" <LittleDanEhren at yahoo.com> wrote in message
news:711c7390.0401061452.3ee3789f at posting.google.com...

> One of the most common bugs that people have on the Python Tutor list
> are caused by the fact the default arguments for functions are
> evaluated only once, not when the function is called. The solution to
> this is usually to use the following idiom:

[snip]

It's an interesting proposal, but outside of the possible
breakage, there is one really major problem:

Where is the variable going to come from on the function
call? If it comes from the original context, it's static so why
bother, and if it comes from the caller's context, you're
causing a *huge* amount of coupling, as well as a substantial
slowdown to do a search of the calling chain and the global
context.

You can't even make it optional: use the original
value if it's not defined in the caller's context. If you
do that, you're still exposing the names to the caller
as names he has to avoid when writing the calling
routine.

To put a somewhat positive spin on it though,
it is a significant novice problem. Given that you
don't want to change the language semantics, what's
the next possible way to fix it?

Compare PEP's 215 and 292. Both of these
do dynamic variable fills, but they have one
critical difference: the template is visible, and not
off in a function or method definition somewhere.
Even so, there are significant issues that have to
be resolved.

John Roth


>
> Daniel Ehrenberg





More information about the Python-list mailing list