[Zope] Script (Python) function definitions

Evan Simpson evan@4-am.com
Thu, 12 Jul 2001 13:47:00 -0400


Michael Casaday wrote:

>  def funcA():
>   print "<p>Hello, from funcA()!</p>"
> 
>  def funcB():
>   print "<p>Hello, from funcB()!  I also have a message from funcA()...</p>"
>   funcA()


> Apparently, funcB doesn't know where to look for the definition of funcA.

Script bodies aren't like Python modules; They are function bodies.  The 
most direct translation of what you wrote into plain Python is:

def myScript():
   def funcA():
     print "<p>Hello, from funcA()!</p>"

   def funcB():
     print "<p>Hello, from funcB()!  I also have a message from 
funcA()...</p>"
     funcA()

   funcB()
myScript()

When funcB() executes, it can't find "funcA" in the local or global 
namespaces, since it's only defined in the body of myScript().  Python 
2.1 supports nested scopes, so your example will work as expected in 
Zope 2.4.

Cheers,

Evan @ digicool