An annoyance in Python's attribute access rules

January 29, 2008

When looking up names on objects, I really wish that Python had a special attribute accessor for looking up attributes it was about to call, instead of only running them through the regular attribute lookup chain.

While this uniformity probably has little effects on most code, it complicates the life of things such as proxy objects that trace and monitor access to objects, which sometimes want to behave differently for 'var = you.foo' than for 'var = you.foo()'. You can fake this by returning a proxied foo object that has a __call__ method, but things can rapidly become complicated; for example, what objects do you proxy this way and what objects do you return as is?

Unfortunately, changing this is one of those simple looking things that strike to the heart of a language's conceptual model. Unless you change Python's conceptual model of attribute access, a special __getmethod__ accessor can only be a hack; it triggers only for certain syntactic patterns of method calls, whereas the __call__ approach is fully general.

Sidebar: implementation complications

Adding such an attribute accessor would also have implementation complications in the Python bytecode compiler and interpreter. Right now, all object attributes are retrieved with a single bytecode instruction, LOAD_ATTR, and the LOAD_ATTR that gets the function object may be some distance from the actual calling of the function, because the bytecode is stack based and puts the function arguments on the top of the stack. The easiest way out would probably be to add a new 'load attribute to be called' instruction and make the compiler generate it in the appropriate situation.


Comments on this page:

From 65.172.155.230 at 2008-01-30 13:31:18:

How would you deal with properties if you did that?

By cks at 2008-01-30 16:35:54:

I don't think that properties could change; the CPython core would still wind up calling a property's __get__ even if it is about to call the retrieved object. I don't think that this would be inconsistent, since properties don't use the normal attribute access functions anyways.

Written on 29 January 2008.
« One reason I like Python
Linux's IP forwarding settings summarized »

Page tools: View Source, View Normal, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Tue Jan 29 23:58:43 2008
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.