Magic Module Methods?

John Roth johnroth at ameritech.net
Sat Oct 20 22:38:37 EDT 2001


"Magnus Lie Hetland" <mlh at idi.ntnu.no> wrote in message
news:9qsmsg$afb$1 at tyfon.itea.ntnu.no...
>
> I just found myself needing (or wanting) a module which
> could have default values for variables that weren't set;
> this can be done with Alex's object-as-module trick, but
> it occurred to me, why should this be necessary? All I
> wanted was to add a __getattr__ method to the module...
> Why should I have to replace the module with some other
> object to do that? Can't modules be more like objects in
> this respect? I mean, why not simply allow this:
>
> def __getattr__(self, name):
>     print 'Accessing attribute %s of module %s' (name, self)

I think you're using the wrong tool here. If you want something
similar to __getattr__, just define a function that the client
needs to call to access one of the defaults, and then have
that function access the __dict__ attribute of the module.

It would require refactoring the clients to call the
function, but IMO that would be clearer in any case.

John Roth






More information about the Python-list mailing list