New PEP: Attribute Access Handlers

Paul Prescod paul at prescod.net
Sat Jul 22 12:35:27 EDT 2000


Bjorn Pettersen wrote:
> 
> ...
>
>     def __getattr__(self, a):
>       if a == 's':
>         return self.__dict__['_s'].value()
>       else:
>         return self.__dict__[a]

This is exactly the kind of code that would be simplified.
 
> so client code could continue looking like:
> 
>   myObject = MyObject('foobar')
>   s = myObject.s

Right. Exactly!
 
> > That was actually the first proposal. But inheritance could get
> > confusing if you inherited set without get or get without set etc.
> 
> Hmmm... I'm not quite sure I understand... With __set/get/del_XXX__
> defined in a superclass you would automatically inherit all of them and
> could choose to only override one of them, e.g. if you wanted to perform
> further checks on a set but the get was the same.  With __attr_XXX__ you
> would have to explicitly call the superclass' __attr_XXX__ method if you
> didn't define all of set/get/del(?)  I have a feeling I'm missing
> something...

Well there are two halves to it, interface and implementation. I'll ask
your opinion on the first and then you can tell me if you think you can
find a good implementation.

Consider this module and tell me what it would output (in your opinion):

class A:
    def __set_x__( self, val ):
        print val
a=A()
a.x=5
print a.x
del a.x

class B(A):
    def __get_x__( self, val ):
       return "funny walk"

b=B()
b.x=5
print b.x
del b.x

Now, how would you implement it?

-- 
 Paul Prescod - Not encumbered by corporate consensus
New from Computer Associates: "Software that can 'think', sold by 
marketers who choose not to."




More information about the Python-list mailing list