473,468 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Modules and descriptors

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....+or+properties

http://groups.google.com/group/comp....ule+properties

http://groups.google.com/group/comp....odule+__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.
Jan 7 '08 #1
1 1184
Chris Leary wrote:
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::
>>class C(object):
... pass
...
>>c = C()
exec '''
... 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::
>>c.foo()
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
Jan 8 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Denis S. Otkidach | last post by:
I've noticed that the order of attribute lookup is inconsistent when descriptor is used. property instance takes precedence of instance attributes: >>> class A(object): .... def...
2
by: François Pinard | last post by:
This question is a bit technical, but hopefully, this list will offer me good hints or nice solutions. Happily enough for me, it often does! :-) I would need to recognise and play with...
14
by: Antoon Pardon | last post by:
Can anyone explain why descriptors only work when they are an attribute to an object or class. I think a lot of interesting things one can do with descriptors would be just as interesting if the...
14
by: T. Crane | last post by:
Hi, When troubleshooting code that's saved in a text file, I often find that I want to make a change to it, re-save it, then reimport it. However, just typing import myTestCode doesn't...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.