How to pass a reference to the current module

Peter Otten __peter__ at web.de
Sat Aug 4 06:07:24 EDT 2007


James Stroud wrote:

> Yes, those were typos. And to be consistent, the whole listing of the
> configuration file should be (note: 'doit'->do_something_with):
> 
> [foo]
> param1 = float
> param2 = 4
> 
> [option1]
> __module__ = UserDefined1
> __function__ = do_something_with
> param1 = str
> param2 = 30.0
> 
> [doit]
> param1 = float
> param2 = float
> 
> [etc.]
> 
> 
> With the user's main having the following in it:
> 
> from UserDefined1 import some_function
> def foo(): [etc.]
> def doit(): [etc.]


Why don't you just let your userse write

def foo(param1=float, param2=4):
    ...

and dump the config file? Alternatively you can devise a decorator

@declare(float, int)
def foo(param1, param2=4):
    ...

The problem of mapping names to values, be they modules or functions, will
magically vanish.

Peter



More information about the Python-list mailing list