Bug or Feature?

Stephan Diehl stephan.diehl at gmx.net
Mon Nov 24 04:24:08 EST 2003


I was playing around with defining new types and have seen the
following behaviour:

Python 2.3.1
============
 
>>> class Int(int):pass
...
>>> a = Int(7)
>>> b = Int(8)
>>> c = a + b
>>> type(c)
<type 'int'>

Basicly: Int is not closed under it's defined operations. :-(


by contrast:

>>> class myset(Set):pass
...
>>> a = myset([1,2])
>>> b = myset([2,3])
>>> c = a & b
>>> type(c)
<class '__main__.myset'>

subclasses of Set are closed under the set operations. :-)

Should I consider this as a bug or something I have to live with?

Example, why this can be problematic:
Consider, I want to define an integer subtype that holds integers modulo N.

The straight forward way would be:

class mod7(int):
    def __new__(cls,value):
        return int.__new__(cls,value%7)

Since addition, substraction, etc. doesn't work as expected, one must
redefine all relevant operators. 
This is not what I'd consider object oriented behaviour.

Stephan

P.S.: for the mind police out there: Other than that, Python is great.







More information about the Python-list mailing list