[Zope] DTML methods bafflement
Pavlos Christoforou
pavlos@gaaros.msrc.sunysb.edu
Thu, 15 Apr 1999 17:57:18 -0400 (EDT)
On Thu, 15 Apr 1999, Martijn Faassen wrote:
> Yes, but am I indeed using that part of Python? I was thinking I was
> using (the equivalent of) this:
>
> def callee(a, b, c): # (could also use default initializers here)
> print a, b, c
>
> callee(a="foo", b="bar", c="baz")
Boy how stupid of me!
My answer has no relevance to your question! It is these nice spring days
that make me write them. Don't blame me ;-)
On Thu, 15 Apr 1999, Paulo Eduardo Neves wrote:
>
> In python it just "looks like" the default values are evaluated at
> "compilation" time. The above behavior is due to how python manages
> atribution.
Actually they do get evaluated at 'compilatio' time.
From the python reference manual:
Default parameter values are evaluated when the function definition is
executed. This means that the expression
is evaluated once, when the function is defined, and that that same
``pre-computed'' value is used for each call. This
is especially important to understand when a default parameter is a
mutable object, such as a list or a dictionary: if
the function modifies the object (e.g. by appending an item to a list),
the default value is in effect modified. This is
generally not what was intended.
I remember at one point Tim Peters wrote an example to demonstrate some
issues relevant to the above which included some nested class and function
definitions. He claimed that if anyone could predict what will happen when
the code is run then she/he indeed understands what goes on during class
definitions. I don't have the code but I remember at the time I failed
miserably!
Pavlos