Bug or wart? You make the call.

Alex Martelli aleax at aleax.it
Fri Mar 14 09:17:00 EST 2003


Michael Hudson wrote:

> Thomas Wouters <thomas at xs4all.net> writes:
> 
>> It doesn't have an official rationale, but it's easily
>> considered. Python is a very dynamic language. An instance's
>> __class__ and a class's __bases__ can be dynamically changed. It is
>> part of how Python Works.
> 
> Apropos of not much, I was faintly surprised this didn't raise an
> exception:
> 
> />> class C(object):
> |..  def m(self, x):
> |..   print x
> \__
> />> class D(object):
> |..  pass
> \__
> ->> o = C()
> ->> m = o.m
> ->> o.__class__ = D
> ->> m(1)
> 1

Yes, it IS faintly surprising -- one would expect a check for
isinstance(m.im_self, m.im_class), which, here, would fail.

However, the check is apparently performed only for the explicit
first argument when m.im_self is None, no doubt (?) for reasons
of performance.


Alex





More information about the Python-list mailing list