getattr from local scope

Alex Martelli aleaxit at yahoo.com
Sun Apr 23 18:24:28 EDT 2006


Edward Elliott <nobody at 127.0.0.1> wrote:

> rob.haswell at gmail.com wrote:
> > Basically my application has a scheduler which stores names of functions
> > defined in the "schedule" module in a database, to be run on certain
> > days.  Every night I call schedule.RunSchedule, which grabs all the rows on
> > the database that have to be run now, and I want to call the function
> > defined the same module according to that string.
> 
> I know that sys.modules[__name__] gives the module object for the current
> module.  You could do:
> 
> m = sys.modules[__name__]  # M now current module object
> func = m.__dict__ [fs]     # func has function named by string fs
> func()

sys.modules[__name__].__dict__ may be more handily accessed by the
built-in function globals().


Alex



More information about the Python-list mailing list