[Python-Dev] Re: PEP 326 now online

Bob Ippolito bob at redivi.com
Tue Jan 6 23:15:13 EST 2004


> > Module attributes make sense; make them attributes of object has
> > the unfortunate side effect that they will be attributes of *all*
> > objects and that doesn't seem a good idea.
> >
> > The math module is only appropriate if this is primarily about float
> > numbers.  And see PEP 747 in that case.
>
> You mean PEP 754, right?  Just like creating an arbitrarily large
> integer, floating point infinity is also arbitrary, and may not be big
> enough.
>
> I just got a comment from another user suggesting modifying the
> min/max.__cmp__ so that they are the actual minimum and maximum.
>
> An interesting approach, which makes some sense to me.

[[ I apologize in advance for not carrying along the In-Reply-To and 
References headers, I just finally got around to subscribing to this 
list. ]]

This was my suggestion.  I am for this PEP and am willing to write the 
reference implementation for the new min and max builtins if there's 
enough interest.

I have, like some others here, used my own One True Large Object.  I 
think the best reason to have One True Large Object is because you 
can't really compare two implementations of the One True Large Object 
and expect to get a meaningful result out of it.

For the record, my use case had to do with a giant sorted list of 
tuples and the bisect module.  The first element of a tuple was a 
timestamp, the rest of the tuple isn't worth explaining but I never 
wanted to compare against it.  The "database" had two primary 
operations, inserting records *after* a timestamp, and finding every 
record between two timestamps.  Let's take a look:

 >>> from random import randrange
 >>> from pprint import pprint
 >>> from bisect import bisect_left, bisect_right
 >>> lst = [(randrange(10), randrange(10)) for i in xrange(10)]
 >>> lst.sort()
 >>> pprint(lst)
[(0, 2),
  (0, 7),
  (0, 9),
  (4, 6),
  (6, 5),
  (6, 8),
  (6, 9),
  (7, 7),
  (8, 6),
  (9, 5)]
 >>> bisect_left(lst, (6,))
4
 >>> bisect_right(lst, (6,))
4

Well, that doesn't look too useful does it?  In order for bisect_right 
to have a meaningful return value in this case, I need to build an 
object such that it compares greater than any (6, *foo).

 >>> class MaxObject:
...   def __cmp__(self, other):
...     return int(not self is other)
...
 >>> maxobject = MaxObject()
 >>> bisect_right(lst, (6, maxobject))
7
 >>> lst[4:7]
[(6, 5), (6, 8), (6, 9)]

In this particular contrived case, sys.maxint would have worked, but 
the general case needs One True Large Object.  I think One True Large 
Object should be in builtins and should be called 'max'.  Similarly, 
there are probably use cases for One True Small Object, but none that 
I've personally ran into.  However, since 'min' is already in builtins, 
might as well do it for symmetry.

-bob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2357 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-dev/attachments/20040106/be0f57a5/smime.bin


More information about the Python-Dev mailing list