Callable modules?

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Wed Jul 24 20:01:52 EDT 2002


bokr at oz.net (Bengt Richter) writes:
> >> If the main purpose of the module is to provide one function, I think
> >> it's cleaner to be able to import the module and call the function
> >> without special tricks.
> Or just use your own trick, e.g., given
> 
> ----< PR.py >------
> #PR.py for Paul Rubin
> def PR(): return 'Hi Paul'
> -------------------
> 
> Define one-line trick:
>  >>> def PRimport(name, locs=locals()): locs[name] = getattr(__import__(name),name)
>  ...
> 
> And use it thus
>  >>> PRimport('PR')
>  >>> PR
>  <function PR at 0x007D8FD0>
>  >>> PR()
>  'Hi Paul'

But that's another special trick, more hair to remember.  Why not just
do the obvious thing and make modules work the same way as other objects?



More information about the Python-list mailing list