In a message of Sat, 22 Aug 2015 17:00:55 +1000, "Steven D'Aprano" writes:
>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")
>'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 
>mean.
>
>-- 
>Steve

Yes, but I think the OP's problem is that he has a fool for a teacher,
or a course designer at any rate.  For some reason the author thinks
that the fact that max(A, B) == -max(-A, -B) (for integers) is very,
very clever.

And somehow the teacher hasn't learnt that his or her job is to make
students question 'clever programming' while not distroying the
enthusiasm of any students who come up with clever solutions on their
own.  Cleverness is the consolation prize in this business -- what you
want to write is code that demonstrates wisdom, not cleverness.

They are fun to write, though.

But remember:

     Everyone knows that debugging is twice as hard as writing a
     program in the first place. So if you're as clever as you can be
     when you write it, how will you ever debug it?

     — Brian Kernighan The Elements of Programming Style

So the reflex you want to develop is 'I just did something clever.
Hmmm.  Maybe _too_ clever.  Let's see ...'  The cleverer you are as a
person, the more you have to develop this reflex, because after all,
somebody much less clever -- or experienced -- than you are may have
to fix a bug in your code some day.

so the max(A, B) == -max(-A, -B) trick has everything to do with
'Watch me pull a rabbit out of this hat' and nothing to do with
'good programming style'.

Too much education of the sort that rewards cleverness and penalises
wisdom means we end up with a lot of smart people in this world who
have managed to get the idea that 'Wisdom is something that only
stupid people need.  It is optional for smart people, and I am smart
enough to do without!'

Some people _never_ unlearn this one.  My family is, alas, full of them.

Laura

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to