Integer coercion and 2.2 Type/Class changes

Christopher A. Craig com-nospam at ccraig.org
Fri Oct 26 09:44:19 EDT 2001


Consider the following Python 2.2 code:

>>> class foo(int):
...   def __init__(self, n=0):
...     self=n
...   def bar(self): return self+6
... 
>>> t=foo(4)
>>> t.bar()
10
>>> type(t)
<class '__main__.foo'>
>>> type(t+6)
<type 'int'>
>>> type(t+t)
<type 'int'>

This is the expected result, but probably often not the desired one.
So my question is, is there some way, without redefining all of
the numeric operations, to define a subclass that recasts all numeric
operations to it's own type?  


-- 
Christopher A. Craig <com-nospam at ccraig.org>




More information about the Python-list mailing list