[Tutor] Complications Take Two (Long) Frustrations.

Steven D'Aprano steve at pearwood.info
Sat Aug 22 09:00:55 CEST 2015

On Fri, Aug 21, 2015 at 11:29:52PM +0200, Roel Schroeven wrote:
> Joel Goldstick schreef op 2015-08-21 23:22:
> >so:
> >    print -max(-A, -B)
> That's what I mean, yes. I haven't tried it, but I don't see why it 
> wouldn't work.

It won't work with anything which isn't a number:

py> min("hello", "goodbye")

But the max trick fails:

py> -max(-"hello", -"goodbye")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for unary -: 'str'

If you want to write your own min without using the built-in, there is 
only one correct way to do it that works for all objects:

def min(a, b):
    if a < b: return a
    return b

Well, more than one way -- you can change the "a < b" to "a <= b" if you 
prefer. Or reverse the test and use >, or similar, but you know what I 


More information about the Tutor mailing list