Modules and descriptors

Chris Leary cdleary at gmail.com
Tue Jan 8 00:19:52 CET 2008


As I understand it, the appeal of properties (and descriptors in
general) in new-style classes is that they provide a way to
"intercept" direct attribute accesses. This lets us write more clear
and concise code that accesses members directly without fear of future
API changes.

I love this feature of the language, but find that I still have to
call getter/setter methods of module instances. Since module
attributes are accessed by way of __dict__ and the module type has a
valid __mro__, why doesn't the descriptor protocol apply to module
instances?

TIA!

P.S. There is some previous discussion in comp.lang.python about using
a module like a new-style class.

http://groups.google.com/group/comp.lang.python/browse_thread/thread/bce6c6fba7c06e72/b3a5cd102237777d?lnk=gst&q=%09%0D%0AModule+level+descriptors+or+properties

http://groups.google.com/group/comp.lang.python/browse_thread/thread/20c14ffb99f85320/49d0087c269b8296?lnk=gst&q=module+properties

http://groups.google.com/group/comp.lang.python/browse_thread/thread/f41215a8fc475b7c/1690e6ddfbb257c7?lnk=gst&q=module+__call__

The suggested solution is always to provide a class wrapper in the
module itself, and have the module-user instantiate it appropriately.
This is a valid solution, but doesn't ensure that the client will use
it (the descriptor protocol does, as a feature of the language). As a
result, changes like these can break existing code that uses the
module.



More information about the Python-list mailing list