How can implement the equivalent of a __getattr__
on a class, on a module?
Example
When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method on it with the same name as failed in the attribute lookup on the module.
class A(object):
def salutation(self, accusative):
print "hello", accusative
# note this function is intentionally on the module, and not the class above
def __getattr__(mod, name):
return getattr(A(), name)
if __name__ == "__main__":
# i hope here to have my __getattr__ function above invoked, since
# salutation does not exist in the current namespace
salutation("world")
Which gives:
matt@stanley:~/Desktop$ python getattrmod.py
Traceback (most recent call last):
File "getattrmod.py", line 9, in <module>
salutation("world")
NameError: name 'salutation' is not defined
Here's my own humble contribution -- a slight embellishment of @Håvard S's highly rated answer, but a bit more explicit (so it might be acceptable to @S.Lott, even though probably not good enough for the OP):
In some circumstances the
globals()
dictionary can suffice, for example you can instantiate a class by name from the global scope:Similar to what @Håvard S proposed, in a case where I needed to implement some magic on a module (like
__getattr__
), I would define a new class that inherits fromtypes.ModuleType
and put that insys.modules
(probably replacing the module where my customModuleType
was defined).See the main
__init__.py
file of Werkzeug for a fairly robust implementation of this.