Erm... might I suggest... (Re: New PEP: Attribute Access Handlers)

Greg Ewing see at my.signature
Mon Jul 24 02:05:59 EDT 2000


Gordon McMillan wrote:
> 
> Paul forgot to tell you that the first
> proposal said that defining one or more of __set/get/del_XXX__
> automatically defines all 3, with default behavior of "you can't do that".

I really like the idea of accessor functions, but I
think the current proposal is marred by some less-than-
desirable features that seem to be artifacts of the
proposed implementation.

I assume that the main reason for funnelling all three
accessors through a single function stems from the desire to
be able to look up the function using an interned string,
and if it were necessary to turn "foo" into "__get_foo__" etc.
on the fly, that wouldn't be possible.

However, this could be easily dealt with by keeping a cache
of translations from attribute names to interned accessor names.

So, I'd like to suggest a much simpler (to my way of thinking)
version of the proposal, in which there are three completely
independent accessor functions for each attribute, which behave
in the normal way with respect to inheritance, etc.

The advantages I see are:

1. Simpler to explain and understand.
2. More efficient - no need to dynamically switch on an operation
   code inside the accessor.
3. Doesn't magically put any names in the class dict that weren't
   explicitly put there by the programmer.
4. As a consequence of 3, doesn't preclude having a real attribute with
   the same name in either the class or instance dict.

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list