[Python-ideas] Module __getattr__ [Was: breaking out of module execution]

Gregory P. Smith greg at krypto.org
Mon Apr 30 23:21:08 CEST 2012

On Fri, Apr 27, 2012 at 6:31 AM, Jim Jewett <jimjjewett at gmail.com> wrote:

> On Wed, Apr 25, 2012 at 2:35 PM, Matt Joiner <anacrolix at gmail.com> wrote:
> > If this is to be done I'd like to see all special methods supported. One
> of
> > particular interest to modules is __getattr__...
> For What It's Worth, supporting __setattr__ and __getattr__ is one of
> the few reasons that I have considered subclassing modules.
> The workarounds of either offering public set_varX and get_varX
> functions, or moving configuration to a separate singleton, just feel
> kludgy.
> Since those module methods would be defined at the far left, I don't
> think it would mess up understanding any more than they already do on
> regular classes.  (There is always *some* surprise, just because they
> are unusual.)
> That said, I personally tend to view modules as a special case of
> classes, so I wouldn't be shocked if others found it more confusing
> than I would -- particularly as to whether or not the module's
> __getattr__ would somehow affect the lookup chain for classes defined
> within the module.
> -jJ

Making modules "simply" be a class that could be subclasses rather than
their own thing _would_ be nice for one particular project I've worked on
where the project including APIs and basic implementations were open source
but which allowed for site specific code to override many/most of those
base implementations as a way of customizing it for your own specific (non
open source) environment.  Any APIs that were unfortunately defined as a
module with a bunch of functions in it was a real pain to make site
specific overrides for.  Anything APIs that were thankfully defined as a
class within a module even when there wasn't a real need for a class was
much easier to make site specific.

But this is not an easy thing to do.  I wouldn't want functions in a module
to need to be declared as class methods with a cls parameter nor would I
want an implicit named equivalent of cls; or does that already exist
through an existing __name__ style variable today that I've been ignoring?
 This could leads to basically treating a module globals() dict as the
class dict which at first glance seems surprising but I'd have to ponder
this more.

(and yes, note that I am thinking of a module as a class, not an instance)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120430/533a8446/attachment.html>

More information about the Python-ideas mailing list