Proposal: min(None, x) and max(None, x) return x

Terry Hancock hancock at anansispaceworks.com
Fri Nov 22 15:20:20 EST 2002


> Dave Brueck wrote:
> > On Fri, 22 Nov 2002, Eric Brunel wrote:
> > > So I told myself: wouldn't it be great if max(None, x) or min(None, x) 
> > > always simply returned x? So I could just write:
> > > In addition, today, min(None, x) and max(None, x) work, but have 
strange 
> > > results: as far as I can see, min(None, x) is always None and max(None, 
x) 
> > > is always x, but this behaviour isn't documented.

> > Sure! Write two functions, and put them in your sitecustomize.py file. No 
> > PEPs to write, no patches to submit, no battles to win, and you get all 
> > the functionality you want _today_.

The thing wanted is pretty easy to achieve. You want to throw out the bad 
data, (represented by None), which is a filtering operation (here I use list 
comps instead of filter(), but either would work):

>>> def Max(*seq):
...     return max([e for e in seq if e is not None])
... 
>>> Max(None, y)
1
>>> def Min(*seq):
...     return min([e for e in seq if e is not None])
... 
>>> Min(None, y)
1

This is particularly handy, since depending on the application, "bad data" 
might be represented by a number of different symbols other than None (though 
None is arguably the "right" one to use).

Cheers,
Terry

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list