[Python-Dev] repr(x) != x.__repr__()

Patrick K. O'Brien pobrien@orbtech.com
2002年3月28日 09:46:51 -0600


[Oren Tirosh]
>> 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'
> >>>repr(a)
> 'abc'
> >>>a.__repr__()
> '123'
>
You've got a typo in there. Here is the fixed version:
>>> class A(object):
... def __repr__(self):
... return 'abc'
... 
>>> a = A()
>>> a.__repr__ = lambda: '123'
>>> repr(a)
'abc'
>>> a.__repr__()
'123'
>>>
Of course, the odd behavior is still there.
---
Patrick K. O'Brien
Orbtech

AltStyle によって変換されたページ (->オリジナル) /