How to add a constructor to a subclassed numeric type?
Posted
by abbot
on Stack Overflow
See other posts from Stack Overflow
or by abbot
Published on 2010-04-28T18:45:50Z
Indexed on
2010/04/28
18:47 UTC
Read the original article
Hit count: 210
python
I want to subclass a numeric type (say, int) in python and give it a shiny complex constructor. Something like this:
class NamedInteger(int):
def __init__(self, value):
super(NamedInteger, self).__init__(value)
self.name = 'pony'
def __str__(self):
return self.name
x = NamedInteger(5)
print x + 3
print str(x)
This works fine under Python 2.4, but Python 2.6 gives a deprecation warning. What is the best way to subclass a numeric type and to redefine constructors for builtin types in newer Python versions?
© Stack Overflow or respective owner