dynamically modify help text
Thomas Jollans
thomas at jollans.com
Mon Jun 28 16:46:08 EDT 2010
On 06/28/2010 10:13 PM, Emile van Sebille wrote:
> On 6/28/2010 12:02 PM Benjamin Kaplan said...
>> Just to save the OP some trouble later on: this optimization is done
>> for most of the __*__ methods. Overriding __add__ on an instance won't
>> change the behavior of a + b.
>
> ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
> Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> class Test: pass
> ...
>>>> i = Test()
>>>> i+1
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: unsupported operand type(s) for +: 'instance' and 'int'
>>>> def __add__(self): return 6
> ...
>>>> i.__add__ = __add__
>>>> i+1
> 6
>>>>
>
> Was this in reference to a specific python version?
ahem
Python 2.5.5 (r255:77872, Apr 21 2010, 08:40:04)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):
... def __add__(self, other):
... return "indeed"
...
>>> a = A()
>>> a + 1
'indeed'
>>> def new__add__(other):
... return "not at all"
...
>>> a.__add__ = new__add__
>>> a.__add__(1)
'not at all'
>>> a+1
'indeed'
>>>
>>>
>>> class B:
... def __add__(self, other):
... return 'now this is old-style'
...
>>> b = B()
>>> b+1
'now this is old-style'
>>> b.__add__ = new__add__
>>> b+1
'not at all'
>>>
I hate the fact that Python 2.x has two types of classes. Good riddance.
-- Thomas
More information about the Python-list
mailing list