Defining functions within Script (Python) methods doesn't seem to work very well. This code... 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() return printed ...produces the following error... Error Type: Name ErrorError Value: funcA Apparently, funcB doesn't know where to look for the definition of funcA. If I were to call funcA from the body of the script instead of from a function, the script would execute with no problem. Would anyone be so kind as to share their insight into this matter? Should I not be writing def's into my Python scripts but instead relying solely on other Python scripts for reusable code? I apologize in advance if any of this is covered in the docs. _________________ meeper
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
Michael Casaday writes:
Defining functions within Script (Python) methods doesn't seem to work very well. This code...
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() return printed
....produces the following error...
Error Type: Name ErrorError Value: funcA In Python2.1/Zope 2.4, it will work.
In older Python versions, you have only 2 scopes: function local and module global. The body of a Python script is a Python function body. You are in the function local scope. Inside a local function definition, you see only it own local definitions and the global module ones but not the local definitions in the enclosing scope. This will change with Python 2.1 nested scope support. Dieter
Dieter Maurer wrote:
Michael Casaday writes:
Defining functions within Script (Python) methods doesn't seem to work very well. This code...
def funcA(): print "<p>Hello, from funcA()!</p>"
def funcB():
For python < 2.1 (i.e. zope < 2.4), you can get what you want if you pass funcA to funcB, like this: def funcB(funcA=funcA): -- ................... paul winkler .................... custom calendars & printing: http://www.calendargalaxy.com A member of ARMS: http://www.reacharms.com home page: http://www.slinkp.com
----- Original Message ----- From: "Michael Casaday" <meeper@innerverse.com> Subject: [Zope] Script (Python) function definitions
Defining functions within Script (Python) methods doesn't seem to work very well...
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() return printed
From: "Evan Simpson" <evan@4-am.com>
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()
From: "Dieter Maurer" <dieter@handshake.de>
This will change with Python 2.1 nested scope support.
From: "Paul Winkler" <slinkp23@yahoo.com>
For python < 2.1 (i.e. zope < 2.4), you can get what you want if you pass funcA to funcB, like this:
def funcB(funcA=funcA):
Thanks for all the help! In addition to your comments, it should be noted that (unless you are dumb like me and insist on doing otherwise *grin*) instead of defining the functions in the body of a Python script, one can, of course, write separate Python scripts just for those functions. For instance, if funcB (from my example above) were an actual "Script (Python)" object somewhere else on the site, you could call it by using container.path.to.funcB(). funcB could then successfully call to funcA, which is another separate "Script (Python)" object, by using the same syntax: container.path.to.funcA() _______________ meeper
participants (4)
-
Dieter Maurer -
Evan Simpson -
Michael Casaday -
Paul Winkler