Modules and descriptors

C

Chris Leary

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...t&q= Module+level+descriptors+or+properties

http://groups.google.com/group/comp.../49d0087c269b8296?lnk=gst&q=module+properties

http://groups.google.com/group/comp...7c/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.
 
S

Steven Bethard

Chris said:
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?

Because it doesn't apply to class *instances*. Module-level code gets
executed roughly like this::

mod = ModuleType(...)
exec module_code in mod.__dict__

So consider the similar code::
... pass
... ... def foo(self, *args):
... print self, args
... ''' in c.__dict__

A function ``foo`` has been added to the ``C`` instance. But *instances*
don't invoke the descriptor protocol, so we the ``self`` parameter is
not bound to the ``C`` instance::
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: foo() takes at least 1 argument (0 given)

So the straightforward answer to your question is that module instances
don't invoke the descriptor protocol because instances in general don't
invoke the protocol (only classes do).

The follow-up question is usually something like "well, can't we make
module instances special and have them invoke the descriptor protocol?"
Yes, in theory it would be possible, but it would break backwards
compatibility in very bad ways. For example, every pure function you
define at the module level would then become a bound method whenever it
was accessed. Consider a simple module ``mod`` like::

def foo(bar):
return 'baz(%s)' % bar

If I try to use this module, and module instances invoke the descriptor
protocol, then ``bar`` will always be bound to the module instance. That
means we'd have to totally change how we right module level functions,
and they'd have to start looking like this::

def foo(mod, bar):
return 'baz(%s)' % bar

That, of course, would break *tons* of existing code.

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top