Re: Repost: function/method default-values bug(?)

Kenneth Manheimer (klm@nist.gov)
Wed, 27 Jul 1994 17:05:04 GMT

Some correspondance with someone on the net about this issue helped
clarify my thinking.

I wrote:

> Evidently the default value is only evaluated once, when the function
> is defined, and that same object is used for all invocations. More
> evidence:
> [...]

Here's more conclusive evidence:

>>> def squawk(msg): print msg; return 1
...
>>> def defaulting(y = squawk('Hellooo!')): return y
...
Hellooo!
>>> defaulting()
1

Notice the 'Hellooo!' printed (squawk's side effect) when the
defaulting function was being defined, but not when it was invoked.

I assume it makes sense for value defaulting to be treated like
regular assignments within the context of functions, and get evaluated
on function application, not during function definition. (The
difference from regular assignments is that default values should only
be evaluated when necessary, not when an actual value is provided.)
Is there any reason to not work this way?

Ken
ken.manheimer@nist.gov, 301 975-3539