How do I access a main frunction from an import module?

Jim jscrerar at compuserve.com
Sat Nov 25 01:22:07 CET 2006


Steve wrote:
> This is an interesting question.  It almost looks like a case of
> event-driven programming, where main is the plug-in and abc is the
> framework.
> http://eventdrivenpgm.sourceforge.net/
>
> So how about something like this:
>
> ################## abc.py ####################
>
> #------------------------------------------------------------
> # an "abstract" function.
> # It should be over-ridden in the calling program
> #------------------------------------------------------------
> def m():
> 	raise AssertionError("You should have over-ridden abstract function
> m()")
>
> def a():
>     m()
>     return None
>
>
> ########### main.py ####################
> import abc  # "instantiate" the framework
>
> # define our our "concrete" function m
> def m():
>     print 'something'
>     return None
>
> #-----------------------------------------------
> # override the "abstract" function abc.m()
> # with our own "concrete" function m().
> # Comment out this line and see what happens.
> #-----------------------------------------------
> abc.m = m
>
> # invoke the a() function in the abc framework
> abc.a()
>
> #################################################

Thank you Steve.

You are correct, the project that I'm working on is an event drive
program.  And your solution works perfectly for this simple example.
My poroject does actually have what you called an "abstract" function.
Now I will know try to implement it into my project.

Thanks again.
Jim




More information about the Python-list mailing list