classes (was Re: Same again please for OOP)

D-Man dsh8290 at rit.edu
Thu Dec 28 00:14:37 CET 2000


(Sorry if this posts twice, but I don't see my earlier post)

Originally I was in agreement with Erno, but Alex has convinced me
that Python has a better way.  I think it would be really cool if the
interpreter would automatically look for the __get_* function when a
client access a member, but for now Alex's solution will have to
suffice.

However, I can't get this sample code to work.  I keep getting
AttributeError's or infinitely recursive __getattr__ function.  If I
remove one of the leading underscores it works fine.  This is because
of the current name mangling used to "enforce" private members.

Am I missing something here, or should I only use 1 leading
underscore?

Thanks,
-D

On Mon, Dec 25, 2000 at 09:20:51AM +0100, Alex Martelli wrote:
<snip>
> class AccessorsMixin:
>     def __getattr__(self, name):
>         if name.startswith('__'): raise AttributeError,name
>         return getattr(self, '__get_'+name)()
> 
> and implement method __get_location(self) the same way as you'd
> implement location(self) in your style.  What could be easier?  If
> you already have a __getattr__, it's equally OK to add this couple
> of lines to it, or delegate to this one (delegating has pluses).
<snip>




More information about the Python-list mailing list