Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Pre-PEP: Dynamically evaluating default function arguments

Thread Tools

Pre-PEP: Dynamically evaluating default function arguments

Daniel Ehrenberg
Posts: n/a
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:

def write_stuff(stuff=None):
"""Function to write its argument.

Defaults to whatever is in the variable "things"
if stuff is None:
stuff = things
print stuff

However, it would be much more intuitive and concise if the following
could be done instead, but with the guarantee that changes in the
variable 'things' will be noticed:

def write_stuff(stuff=things):
print stuff

This would instead print whatever is in things when the function is

The goal of this pre-PEP is to make it so that the default arguments
are evaluated dynamically instead of when the function is created. As
this is a pre-PEP, the details have not been finalized yet. One of the
first details is whether or not the default argument should be checked
to make sure it exists before the function is called. I think it would
make the most sense if things were evaluated when the function was
defined but the result was not saved and was reevaluated whenever the
function is called. This might not be the best way to do it, though,
and it might be better to treat it just like a regular equals sign.

As with any additional dynamic properties added, there is the
possibility of some errors to go uncaught and some. Here is an example
of some hypothetical (but unlikely) code that would cause an error as
a result of this change:

>>> x = 5
>>> def return_something(stuff=x):

return stuff
>>> return_something()

>>> x += 1
>>> return_something() #dynamic now

>>> del x
>>> return_something()

Traceback (most recent call last):
File "<input>", line 1, in ?
NameError: name 'x' is not defined

To me, this seems like the logical behavior, but there is still the
possiblility of some programs being broken. A way to preserve
backwards compatability could be to store the initial evaluation of
the default and use it to fall back on if the variable needed isn't
around anymore, but this seems overly complicated.

Daniel Ehrenberg
Reply With Quote
Jeff Epler
Posts: n/a
That proposal gets "-All" from me. (or, at least, I think that's what I
mean. Maybe I just mean "None", I couldn't stand that other thread)

Reams of code depend on default arguments working as they do today. For
l = []
for i in range(10):
l.append(lambda x, y=i: x+y)
def fib(x, y={0: 1, 1: 1}):
assert x >= 0
if not y.has_key(x):
y[x] = fib(x-1) + fib(x-2)
return y[x]
def stack():
def push(x, l=[]):
def pop(l=push.func_defaults[0]):
return l.pop()
return push, pop

It's not clear exactly how this feature would work. What additional
state will you store with function objects to make it work? Right now,
it works like this:
def f(l=x):
is equivalent to
_magic = x
def f(*args):
assert len(args) < 2
if args: l = args[0]
else: l = _magic
you're proposing something more like
_magic = lambda: x
def f(*args):
assert len(args) < 2
if args: l = args[0]
else: l = _magic()
you've just added one additional function call overhead for each
defaulted argument, plus the time to evaluate the expression 'x', even
in cases where it makes no difference. Here's a case where the
programmer has performed a micro-optimization that will probably be
slower after your change (even though the program's meaning is not
def log10(x, l=math.log, l10=math.log(10)):
return l(x)/l10

That leaves the case where you actually want the default argument value
to depend on the current program's state, as below:
def log(s, when=time.time()):
print >>stderr, time.asctime(when), s
I think it's better to write
def log(s, when=None):
if when is None: when = time.time()
print >>stderr, time.asctime(when), s
because it makes calling code easier, too:
def log_with_prefix(p, s, when=None):
log("%s: %s" % (p, s), when)
instead of repeating the default argument everwhere you may call it both ways
def log_with_prefix(p, s, when=time.time()):
log("%s: %s" % (p, s), when)
or having to test at each place you call:
def log_with_prefix(p, s, when=None):
if when is None:
log("%s: %s" % (p, s))
log("%s: %s" % (p, s), when)


Reply With Quote
John Roth
Posts: n/a
"Daniel Ehrenberg" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...

> 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:


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

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

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

Reply With Quote
Daniel Ehrenberg
Posts: n/a
I'm sorry I wasted your time, everyone.
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
Evaluating a Function After X Seconds: Python Equivalent to JavaScript's SetTimeout() Function Python 3 06-12-2006 07:45 AM
RE: Pre-PEP: Dynamically evaluating default function arguments Robert Brewer Python 3 01-08-2004 05:55 PM
Question about evaluating the arguments of printf (maybe OT?) Edith Gross C++ 2 11-02-2003 12:16 PM