[Tutor] this group and one liners
Dennis Lee Bieber
wlfraed at ix.netcom.com
Thu Jul 7 18:50:29 EDT 2022
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/
More information about the Tutor
mailing list