problem with variable and function

Chris Rebert clp2 at rebertia.com
Sun Mar 14 19:48:18 CET 2010


On Sun, Mar 14, 2010 at 10:26 AM, Alex Hall <mehgcap at gmail.com> wrote:
> Hi all,
> I have a file with a dictionary and a function. The dictionary holds
> the name of the function, and the function references the dictionary.
> If I put the dictionary first, the function is happy but the
> dictionary says the function is not defined. If I switch the two and
> put the function first, the function says the dictionary does not
> exist. Does anyone have an idea as to how I can make both of them
> happy?
<snip>
> Reverse it, though:
>
> def myFunc():
>  myOtherVar=myVar
>
> myVar={
>  1:myFunc
> }
>
> and the function myFunc does not see the dictionary.

Please be more specific in what you mean by it not "seeing" the
dictionary, because the "reversed" approach *should* work:

$ python
Python 2.6.4 (r264:75706, Feb 25 2010, 01:21:39)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
...     bar = baz
...     print bar
...
>>> baz = {1:foo}
>>> foo()
{1: <function foo at 0x37b870>}

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list