replacing instance __setattr__

Jonathan Hogg jonathan at onegoodidea.com
Thu Jul 4 12:28:28 EDT 2002


On 4/7/2002 12:42, in article K3QTagAQSDJ9Ew27 at jessikat.demon.co.uk, "Robin
Becker" <robin at jessikat.fsnet.co.uk> wrote:

> special methods aside I find in 'Unifying types and classes in Python
> 2.2' http://www.python.org/2.2/descrintro.html#mro
> that the object is always searched last for methods which seems a bit
> daft and obviously different for non-method attributes.
> 
> Maybe that's not true of the 2.2.1 interpreter.

I get:

>>> class foo:
...     def hello( self ):
...         print 'foo!'
... 
>>> f = foo()
>>> f.hello()
foo!
>>> 
>>> def bar_hello( self ):
...     print 'bar!'
... 
>>> f.hello = new.instancemethod( bar_hello, f, foo )
>>> f.hello()
bar!
>>> 

As far as I am aware this has always been the case. I think you may have
misread that document. The section you reference refers to the method
resolution order, which is used in searching the base classes after
consulting the instance dict. In new style classes, the 'object' type is
generally the last type in the MRO, perhaps that's what you're thinking of?

> I had always imagined that the object dict was used before anything else
> in all cases. We seem to have a lot of special cases for the unwary to
> find punji sticks in.

There's only one special case that I know of and that's the special
attribute methods, which are different for the reason I posted before: they
*are* the mechanism that implements the lookup order.

Jonathan




More information about the Python-list mailing list