New restrain builtin function?

Robert Brewer fumanchu at amor.org
Sat Mar 20 16:41:21 EST 2004


Pierre Rouleau wrote:
> In several occasions, I found myself looking for a function 
> that would take a value and restrict is within a specified
> set of boundaries...
> 
> def restrain(value, theMin, theMax) :
> 
>      assert(theMax >= theMin)
>      return min(max(value,theMin),theMax)
> 

I'd find a limit() function more useful, where either min or max is
optional:

def limit(value, lower=None, upper=None):
    """Return value limited by lower and/or upper bounds."""
    
    if upper is not None:
        value = min(value, upper)
    if lower is not None:
        value = max(value, lower)
    return value

This allows for single-boundary limits:

>>> limit(3, 5)
5
>>> limit(3, upper=1)
1

And a no-op:

>>> limit(3)
3

Also, you don't need to assert that upper >= lower; in fact, if upper is
less than lower, you can use the same function for exclusive boundaries:

>>> limit(3, 5, 1)
5

However, I find both restrain(3, 1, 5) and limit(3, 1, 5) difficult to
read. It would be nice to produce the same thing with some other syntax.
If we did it in C, we could at least write: limit(lower=None, value,
upper=None). But perhaps there's some other means I'm not thinking of.
It'd be nice to have "value" look different than upper and lower. Maybe
something like: limit(value, (lower, upper)), or even value.limit(lower,
upper) using a __limit__ customizable method. Hmm.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list