[Tutor] this group and one liners

avi.e.gross at gmail.com avi.e.gross at gmail.com
Thu Jul 7 18:01:13 EDT 2022


Albert,

 

It would be a great idea to use:

 

Max(item or 0)

 

But if item == [] then 

 

max([] or 0) 

 

breaks down with an error and the [] does not evaluate to false.

 

Many things in python are truthy but on my setup, an empty list does not seem to be evaluated and return false before max() gets to it.

 

Am I doing anything wrong? As noted, max has an argument allowed of default=0 perhaps precisely as there isn’t such an easy way around it.

 

Avi

 

 

From: Albert-Jan Roskam <sjeik_appie at hotmail.com> 
Sent: Thursday, July 7, 2022 10:20 AM
To: avi.e.gross at gmail.com
Cc: tutor at python.org
Subject: Re: [Tutor] this group and one liners

 

 

 

On Jul 6, 2022 17:50, avi.e.gross at gmail.com <mailto:avi.e.gross at gmail.com>  

As has often been discussed, quite a bit is done in languages like python using constructs like:

[ x+2 for x in iterator ]

 

======

My rule of thumb is: if black converts a list/set/dict comprehension into a multiline expression, I'll probably refactor it. I try to avoid nested loops in them. Nested comprehensions may be ok. Nested ternary expressions are ugly, even more so inside comprehensions!

 

 

Specifically with some of the code I shared recently, I pointed out that if max() failed for empty lists as an argument, you could simply create a safe_max() function of your own that encapsulates one of several variations and returns a 0 when you pass it something without a maximum.

 

====

Or this?

max(items or [0])

 



More information about the Tutor mailing list