Calling a function from another function

I’m having a problem structuring my Jython code. What I’m trying to do is call one function from another function, but I keep getting a NameError.

I’m using the following code:[code]def A():
print B()
return

def B():
return “blah”

A()
[/code]Any ideas how to do this?

Al

Python only ever has a local and global namespace. In your example B was never defined in the namespace of A. Here’s your code using local namespaces.

def A(): def B(): return "blagh" print B() return A()
You’re likely using this in a component action script. I’m not sure that you can easily reference that namespace, even from what would seem to be a “sibling” namespace (remember only 2, not a hierarchy).

Use Script Modules (Project->Script Modules or cntl+M) to make functions globally accessible. For example, create a module called test under the app package with the following code:

def A(): import app.test print app.test.B() return def B(): return "Blah"
On the component action, use either:

app.test.A()
print app.test.B()

Keep in mind that the app and fpmi packages have been pre-imported for you on component Actions. You need to import packages in order to call their functions within the Script Modules section.

Nathan’s post is right on. I just wanted to add that because functions are objects in Python, they can be passed around. So you could have modified your code to this and had it work:

[code]def A(funcToCall):
print funcToCall()
return

def B():
return “blah”

A(B)[/code]

Thanks for the excellent replies. Both approaches (putting the function in a global script and passing the function as an object) work perfectly.

Al