I was playing around with defining new types and have seen the
following behaviour:
Python 2.3.1
============
[color=blue][color=green][color=darkred]
>>> class Int(int):pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> a = Int(7)
>>> b = Int(8)
>>> c = a + b
>>> type(c)[/color][/color][/color]
<type 'int'>
Basicly: Int is not closed under it's defined operations. :-(
by contrast:
[color=blue][color=green][color=darkred]
>>> class myset(Set):pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> a = myset([1,2])
>>> b = myset([2,3])
>>> c = a & b
>>> type(c)[/color][/color][/color]
<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,val ue):
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.
following behaviour:
Python 2.3.1
============
[color=blue][color=green][color=darkred]
>>> class Int(int):pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> a = Int(7)
>>> b = Int(8)
>>> c = a + b
>>> type(c)[/color][/color][/color]
<type 'int'>
Basicly: Int is not closed under it's defined operations. :-(
by contrast:
[color=blue][color=green][color=darkred]
>>> class myset(Set):pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> a = myset([1,2])
>>> b = myset([2,3])
>>> c = a & b
>>> type(c)[/color][/color][/color]
<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,val ue):
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.
Comment