Callable modules?
Alex Martelli
aleax at aleax.it
Mon Jul 22 01:42:24 EDT 2002
Martin v. Loewis wrote:
> Paul Rubin <phr-n2002b at NOSPAMnightsong.com> writes:
>
>> Is there a way to do that?
>
> You can put a callable object in sys.modules. Module objects
> themselves are not callable.
Right. E.g., the module could be:
import sys
class Callamod(object):
def __call__(self): print "see, I'm callable!"
def __getattr__(self, name):
if name[:1].lower()=='x': return hex(name[1:])
raise AttributeError, name
sys.modules[__name__] = Callamod()
This shows off the possibility of having dynamic get-attr
functionality, as well as callability, in what every other
piece of code accesses as "a module".
Only limitation I know of -- no reload() on this kind of
"module"... reload(x) needs x to be a module object. Not
sure if you could inherit Callamod from the module type
to work around this.
Alex
More information about the Python-list
mailing list