[Tutor] this group and one liners
avi.e.gross at gmail.com
avi.e.gross at gmail.com
Thu Jul 7 19:36:45 EDT 2022
Thanks, Dennis. Indeed I should be using a list that contains a single 0
(albeit many works too),
max([] or [0])
Amusingly, I played around and max() takes either a comma separated list as
in max(1,2) or it takes any other iterable as in [1,2] and I think what is
happening is it sees anything with a comma as a tuple which is iterable. I
mean this works:
max(1,2 or 2,3)
returning 3 oddly enough and so does this:
max([] or 0,0)
returning a 0.
-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of
Dennis Lee Bieber
Sent: Thursday, July 7, 2022 6:50 PM
To: tutor at python.org
Subject: Re: [Tutor] this group and one liners
On Thu, 7 Jul 2022 18:01:13 -0400, <avi.e.gross at gmail.com> declaimed the
following:
>
>max([] or 0)
>
>
>
>breaks down with an error and the [] does not evaluate to false.
>
That's because the 0 is not an iterable, not that [] didn't
evaluate...
>>> [] or 0
0
>>> max([] or 0)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> max(0)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>
max() requires something it can iterate over. The expression
<something falsey> or <anything>
returns <anything>, not true/false, and does NOT override max() normal
operation of trying to compare objects in an iterable..
>>>
>>> [] or "anything"
'anything'
>>> max([] or "anything")
'y'
>>>
The "default" value option applies only if max() would otherwise
raise an exception for an empty sequence.
>>> max([], default=0)
0
>>> max([])
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: max() arg is an empty sequence
>>>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list