How to represent the infinite ?
Cliff Wells
logiplexsoftware at earthlink.net
Thu Jul 4 20:32:28 EDT 2002
On Thu, 2002-07-04 at 02:42, Steve Tregidgo wrote:
> Cliff Wells <logiplexsoftware at earthlink.net> wrote in message news:<mailman.1024600723.6989.python-list at python.org>...
> > from __future__ import generators
> >
> > class Infinity(int):
> > def __init__(self):
> > int.__init__(self)
> > self.sign = 1
> >
> > def __str__(self):
> > return "%sInfinity" % {-1: "-", 1: ""}[self.sign]
> >
> > def __repr__(self):
> > return "<%s>" % self
> [followed by many other good things]
>
Thanks!
> Nice implementation. May I suggest two small changes:
>
> def __init__(self, sign=1):
> int.__init__(self)
> self.sign = sign
>
> def __repr__(self):
> return "%s(%s)" % (self.__class__.__name__, self.sign)
>
> It's partially tested (my version of Python doesn't have generators...
> I really should get with the times); the point is that, assuming
> Infinity is available in the calling namespace, this will work:
>
> Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> from some_module import Infinity
> >>> i = Infinity()
> >>> repr(i)
> 'Infinity(1)'
> >>> j = eval(repr(i))
> >>> j == i
> 1
> >>> k = -eval(repr(i))
> >>> k + i
> 0
Looks good to me. I was more concerned with the other stuff and kind of
stuck the __repr__ implementation in as a placeholder.
However, I think I prefer this:
class Infinity(int):
def __init__(self):
int.__init__(self)
self.sign = 1
def __repr__(self):
return "%s()" % self
Basically the same effect, but it prints as "-Infinity()" rather than
"Infinity(-1)", not to mention being a bit shorter.
>>> from infinity import Infinity
>>> m = -Infinity()
>>> m
-Infinity()
>>> `m`
'-Infinity()'
>>> str(m)
'-Infinity'
>>> j = eval(repr(m))
>>> j
-Infinity()
>>> j == m
1
>>> k = -eval(repr(j))
>>> k
Infinity()
>>>
> Quoting from the Python reference manual (section 3.3.1):
> > If at all possible, this should look like a valid Python expression
> > that could be used to recreate an object with the same value (given
> > an appropriate environment).
>
> I'd be interested to hear if these minor modifications work with the
> class proper; I had to remove the generator/yield lines, and the
> subclassing from int, to get it to run under 1.5.2 :-)
I can't see how the __repr__ change would have any effect (other than
making it better). Not subclassing from int will be an issue:
>>> from infinity import Infinity
>>> i = Infinity()
>>> i / 1
Infinity()
>>> 1 / i
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for /: 'int' and 'instance'
>>>
But since that was supposed to raise an exception anyway, it may not be
much of an issue (it just raises a less informative exception). The
exception raised by Infinity(int) is:
>>> from infinity import Infinity
>>> i = Infinity()
>>> i / 1
Infinity()
>>> 1 / i
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ZeroDivisionError: integer division or modulo by zero
>>>
Which isn't entirely correct either but a bit more informative.
> Anyway, as I said: I like the class.
It's always fun (and surprisingly easy) to write little things like this
in Python.
> just-wanted-to-join-in-ly y'rs,
> Steve
Regards,
Cliff
More information about the Python-list
mailing list