Chaning instance methods

Vladimir Marangozov Vladimir.Marangozov at inrialpes.fr
Thu Apr 8 10:51:42 EDT 1999


Tim Peters wrote:
> 
> [me, on f being an instance of Foo, becoming an instance of Bar,
>  the latter being derived directly from Foo]
> > ...
> 
> When I signed my exposition of instance-method trickery "subclassing-is-
> a-lot-easier-ly y'rs", I had exactly this in mind:
> 
> class Bar(Foo):
>     def m(self):
>         print "m2"
> 
> f = Bar()
>

Careful!
We'll eventually see what you had exactly in mind if you change the last
line from

(1) f = Bar()

to

(2) f.__class__ = Bar

While (1) resets f's internal state and makes a brand new instance,
(2) preserves that state (which reflects f's past existence as an
instance of Foo) by changing only its interface to that of Bar
"on the fly". The difference is quite fundamental.

We all knew that you meant (2) in your sig, but making it slightly
more explicit does not hurt :-)

-- 
       Vladimir MARANGOZOV          | Vladimir.Marangozov at inrialpes.fr
http://sirac.inrialpes.fr/~marangoz | tel:(+33-4)76615277 fax:76615252




More information about the Python-list mailing list