classes (was Re: Same again please for OOP)

D-Man dsh8290 at rit.edu
Wed Dec 27 16:20:53 EST 2000


I had agreed with Erno with accessor methods, but Alex has convinced
me that accessing fields as fields is cool with Python's ability to
transparently convert it to a function call (giving the same results
as Erno's accessors and mutators, but allowing better looking client
code).  It would be really cool if the interpreter would do the magic
of looking for __get_attributename automatically, but Alex's solution
will have to do for now.

However, I can't get Alex's example code to work.  I either get
AttributeError or an infinitely recursive __getattr__.  The reason is
if I make a class Foo that has a function __get_value, the name is
mangled to _Foo__get_value.  If I reduce the leading underscores to
only 1 underscore, it works fine.

Have I missed something here Alex?

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