[Python-bugs-list] [ python-Bugs-515336 ] Method assignment inconsistency

noreply@sourceforge.net noreply@sourceforge.net
Thu, 28 Mar 2002 07:33:13 -0800


Bugs item #515336, was opened at 2002-02-09 16:40
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=515336&group_id=5470

Category: Type/class unification
Group: Python 2.2
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Oren Tirosh (oren-sf)
>Assigned to: Guido van Rossum (gvanrossum)
Summary: Method assignment inconsistency

Initial Comment:
Python code and builtin functions don't have a 
consistent view of new style objects when method 
attributes are assigned:

class A(object):
   def __repr__(self):
      return 'abc'

a = A()
a.__repr__ = lambda:'123'

Result: repr(a) != a.__repr__()

The repr() function sees the original __repr__ method 
but reading the __repr__ attribute returns the 
assigned function.  With classic objects both cases 
use the assigned method.




----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2002-03-28 10:33

Message:
Logged In: YES 
user_id=6380

I'm going to reject this bug as "won't fix".  I specifically
put code in the new classes to create this behavior.  It's
partly a
performance hack: many operations become much slower if they
have to
check the instance __dict__ first.  But I also believe it's
poor
style to override a system operation on the instance rather
than on
the class.  If you want to be able to override behavior per
instance,
use this:

  class A(object):
    def __repr__(self):
      return self.repr()
    def repr(self):
      return 'abc'

  a = A()
  a.repr = lambda: '123'
  assert repr(a) == a.__repr__() == '123'


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=515336&group_id=5470