This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Method assignment inconsistency
Type: Stage:
Components: Interpreter Core Versions: Python 2.2
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: gvanrossum, oren-sf
Priority: normal Keywords:

Created on 2002-02-09 21:40 by oren-sf, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Messages (2)
msg9206 - (view) Author: Oren Tirosh (oren-sf) Date: 2002-02-09 21:40
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.


msg9207 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-03-28 15:33
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'
History
Date User Action Args
2022-04-10 16:04:58adminsetgithub: 36075
2002-02-09 21:40:56oren-sfcreate