[Zope] DTML methods bafflement

Paulo Eduardo Neves neves@inf.puc-rio.br
Thu, 15 Apr 1999 17:59:20 -0300


Hi Pavlos,

Pavlos Christoforou wrote:
> In python function arguments that have default values are evaluated at
> 'compilation' time.
> 
...
> 
> >>> b=4
> >>> def callee(a=b):
> ...     print a
> ...
> >>> callee()
> 4
> >>> b=5
> >>> callee()
> 4

In python it just "looks like" the default values are evaluated at
"compilation" time. The above behavior is due to how python manages
atribution.
When I execute the commands:
a = 3
b = a
a = 4

What I'm doing is 
a = 3 #creates an integer object '3' and points a to it
b = a #points b to the same integer object that a points to
a = 4 #creates an integer object '4' and points a to it, b still points
to the old 3 		 


See what happens when you just change the object that is pointed:

>>> b = [3]
>>> def f(a=b):
...   print a
... 
>>> f()
[3]
>>> b[0] = 1
>>> f()
[1]

Confusing, no? I've needed some time to grok it.


-- 
Paulo Eduardo Neves
PUC-Rio de Janeiro 
Pager: Central: 292-4499 cod. 213 99 64
ou use a URL:  http://www.learn.fplf.org.br/neves/mensagempager.html