replacing instance __setattr__

Bernhard Herzog bh at intevation.de
Thu Jul 4 05:49:56 EDT 2002


Robin Becker <robin at jessikat.fsnet.co.uk> writes:

> In article <yAZtHKAwg0I9EwKP at jessikat.demon.co.uk>, Robin Becker
> <robin at jessikat.fsnet.co.uk> writes
> >Is there something intrinsically hard about overriding an instance's special 
> >methods? I'm using
> >python 2.2 and expected that it would be easy to wrap an existing __setattr__, 
> >but
> >it seems the class is searched first not the instance. Must I clone the class 
> >and
> >set the method on that?
> >
> ....
> well it seems Python not as dynamic as I believed. The object is
> searched last which seems completely counter-intuitive to me, but there
> you go.

The truth is a bit more complex. If a class defines or inherits one of
the special methods __setattr__, __getattr__ and __delattr__, the
corresponding unbound methods are stored in the class structure directly
so that they can be accessed quickly. 

There's no way to override them on an instance by instance basis (except
perhaps by deriving an appropriate class from instance.__class__ and
turning the instance into an instance of that class by assigning to
__class__)

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/



More information about the Python-list mailing list