Calling a function in __main__ from a module?

Marc Shapiro mshapiro at sunlitsurf.com
Thu Dec 18 19:40:09 EST 2003


On 18 Dec 2003 19:21:14 +0000, John J. Lee <jjl at pobox.com> wrote:

> Marc Shapiro <mshapiro at sunlitsurf.com> writes:
> [...]
>> Traceback (most recent call last):
>>    File "./hp", line 62, in ?
>>      curses.wrapper(main)
>>    File "/usr/lib/python2.3/curses/wrapper.py", line 44, in wrapper
>>      res = func(stdscr, *rest)
>>    File "./hp", line 58, in main
>>      mb.mainloop(items)
>>    File "/home/mns/menu.py", line 258, in mainloop
>>      exec cmd + "('" + name + "')"
>>    File "<string>", line 1, in ?
>> NameError: name 'NOP' is not defined
>>
>> 'NOP' is the name of the function that I am trying to call.  It does
>> not exist in the menu module, but is in the calling (hp) module.  I
>> can't qualify the name (i.e. __main__.NOP() ) since __main__ can not
>> be used for qualification, only called module names can be used that
>> way.
>>
>> How do I get the module to call functions in a parent module.  Tkinter
>> would set up a callback function and bind it to an event.  Can
>> something similar be done for a console program using curses?
>
> Stop thinking about 'the parent module'.  The question of *which*
> module wants to get at a particular function is irrelevant.  Think
> like this instead: you have a bunch of modules, which any piece of
> Python code is free to import stuff from, and you have a main program
> which can be a module or not as you choose (if it's on sys.path, it's
> a module, if it's not, it ain't; if it *is* a module, you'll also have
> an if __name__ == "__main__" to prevent the program startup code
> getting executed at times other than program startup).
>
> So, you want to get at your NOP function.  Two obvious choices:
>
> 1. import it
>
> 2. pass it as a function argument
>
> If 1, just make sure the file you want to import it from is on
> sys.path (or in a package that's on sys.path; a package is a directory
> on sys.path that contains an __init__.py file).  You can always just
> grab your program startup code (say, main()), and stick it in a tiny
> executable file somewhere convenient like ~/bin, and move any other
> code that was originally in the same file into a module.
>
> BTW, I have never yet used exec or eval, and I've written a fair
> amount (tens of thousands of lines) of Python code.  Whatever you
> think you need them for, you probably don't.
>
>
> John

I was just about to post back that I had solved the problem.  As it turns 
out, I used method 2.  I passed a function argument in place of a string 
with the function name.  Thanks to Python being an untyped language this 
required only a few minor changes in the module and it is now working as 
expected.  My planned next step was, indeed, to see if I could rewrite so 
as to avoid using 'exec'.  It can certainly be confusing to determine what 
is being evaluated by which sections of code and at what time.  I am sure 
that when it comes to maintaining this at a later date that it will be a 
much easier task if I can get rid of the 'exec' lines.

-- 
Marc Shapiro


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 100,000 Newsgroups - 19 Different Servers! =-----




More information about the Python-list mailing list