How to represent the infinite ?
Steve Tregidgo
smst at bigfoot.com
Thu Jul 4 05:42:05 EDT 2002
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]
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
>>> ^Z
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 :-)
Anyway, as I said: I like the class.
just-wanted-to-join-in-ly y'rs,
Steve
--
Steve Tregidgo
More information about the Python-list
mailing list