[Python-Dev] repr(x) != x.repr() (original) (raw)
Guido van Rossum guido@python.org
Thu, 28 Mar 2002 10:28:12 -0500
- Previous message: [Python-Dev] repr(x) != x.__repr__()
- Next message: [Python-Dev] repr(x) != x.__repr__()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
This phenomenon is not really specific to repr(). It occurs with new-style classes when methods are assigned.
Example: class A(object): def repr(self): return 'abc' >>>a = A() >>>a.repr = lambda: 'abc'
You meant '123' there. :-)
>>>repr(a) 'abc' >>>a.repr() '123'
Whenever the method is accessed by Python code it sees the assigned method in the object's dict. Builtins like repr(), iter() etc, see the original class method. With classic objects both Python and C code see the assigned function. This is bug #515336 on sourceforge.
I'm probably 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'
Is anyone aware of any other cases where C code and Python code don't see objects the same way?
Yes, a+b is not the same as a.add(b). a[i:j] is not the same as a.getslice(i, j).
--Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-Dev] repr(x) != x.__repr__()
- Next message: [Python-Dev] repr(x) != x.__repr__()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]