What's the value of "None" between 2.1.1 and 1.5.2

Hans Nowak wurmy at earthlink.net
Thu Nov 29 22:22:26 EST 2001


Ozone Hole near South Pole wrote:
> 
> Hi,
> 
> I have written a simulation in python 1.5.2....  It used to work fine.
> 
> When I logged on a new machine which has Python 2.1.1 installed, my
> simulation
> has gone wild.  Later on, I figured that out one weird thing about the
> value of "None":
> 
> In the new machine:
> Python 2.1.1 (#1, Nov 11 2001, 18:19:24)
> [GCC 2.95.4 20011006 (Debian prerelease)] on linux2
> Type "copyright", "credits" or "license" for more information.
> >>> a=[3,4,5,7,None,0.2,-4]
> >>> print min(a)
> None
> >>> print max(a)
> 7
> 
> Python 1.5.2 (#1, Sep 30 2000, 18:08:36)  [GCC 2.95.3 19991030
> (prerelease)] on linux-i386
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> a=[3,4,5,7,None,0.2,-4]
> >>> print min(a)
> -4
> >>> print max(a)
> None
> >>>
> 
> It seems to me that "None" means -Inf in 2.2.1 but means "Inf" in
> 1.5.2.  Why do the Python guys change this assumption?  In my
> calculations, I use "None" to indicate very large estimation error.
> Is there any formal symbol for this?  I don't really want to fix this
> type of bug again whenever Python upgrades....

None doesn't really have a numerical value; its value is, well,
none! In theory you can stick all kinds of objects in comparisons,
the only problem is that the results may be undefined, and may
change between Python versions or maybe even sessions. In other
words, what happens to work today, might not work tomorrow, in 
cases like this.

Instead of relying on the behavior that None happens to show in
a given version, you could use e.g. 1e1000 for a large number. If
that isn't enough, you could consider rolling your own class that
always compares as larger (or smaller) than another object:

>>> class MuchoGrande:
	def __cmp__(self, other):
		return 1	# larger than anything else
	def __repr__(self):
		return "MuchoGrande"

>>> muchogrande = MuchoGrande()
>>> z = [1, 2, 1e100, muchogrande, -7, -900]
>>> z
[1, 2, 1e+100, MuchoGrande, -7, -900]
>>> z.sort()
>>> z
[-900, -7, 1, 2, 1e+100, MuchoGrande]
>>> min(z)
-900
>>> max(z)
MuchoGrande

HTH,

--Hans



More information about the Python-list mailing list