[IronPython] Calling Methods from other Methods

Shri Borde Shri.Borde at microsoft.com
Fri Dec 22 22:00:36 CET 2006


This is quite non-intuitive, but it is by-design. It is happening because of using a locals dictionary which is different than the globals dictionary. Note that top-level code in a .py file executes with the same locals and globals dictionary.

The following Python snippet shows that a similar issue exists in pure Python code. func1 and func2 get assigned in the locals dictionary. When executing func2, the global dictionary is looked up, and it does not have func1.

>>> g={}
>>> l={}
>>> functions="""def func1(): print 'func1'
... def func2(): func1()
... func2()
... """
>>> exec(functions, g, l)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 3, in ?
  File "<string>", line 2, in func2
NameError: global name 'func1' is not defined
>>> l
{'func2': <function func2 at 0x01D285F0>, 'func1': <function func1 at 0x01D28930>}

The following snippet shows how you can achieve what you are trying to do. The takeaway is that if you use a locals dicitionary different than the globals dictionary, you have to explicitly declare your global variables so that they get assigned in the globals dictionary.

>>> g={}
>>> l={}
>>> functions="""global func1
... global func2
...
... def func1(): print 'func1'
... def func2(): func1()
... func2()
... """
>>> exec(functions, g, l)
func1

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dave Fugate
Sent: Wednesday, December 20, 2006 4:33 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Calling Methods from other Methods

Thanks Ken!

Looks like a bug to me and I've filed a CodePlex Work Item (see http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=6714).

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of kenj55
Sent: Tuesday, December 19, 2006 9:39 AM
To: users at lists.ironpython.com
Subject: [IronPython] Calling Methods from other Methods


Hi,

I'm having a problem calling one method from another when i run python
within the PythonEngine.

i.e. If I have the following code:

string functions = "def func1():\n\treturn \"hello\"\n\n";
functions += "def func2():\n\treturn func1()\n\n";
functions += "func2()";

pyEngine.Execute(functions, module, locals);

I get an error along the lines of:

IronPython.Runtime.Exceptions.PythonNameErrorException: name 'func1' not
defined



Does anyone have any ideas where I might be going wrong?

Cheers,
Ken
--
View this message in context: http://www.nabble.com/Calling-Methods-from-other-Methods-tf2847245.html#a7951284
Sent from the IronPython mailing list archive at Nabble.com.

_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list