[Tutor] this group and one liners

avi.e.gross at gmail.com avi.e.gross at gmail.com
Thu Jul 7 21:52:45 EDT 2022


OK, Alan, in the interest of tying several annoying threads together, I make
a one-line very brief generator to return a darn zero so I can set a default
of zero on an empty list to max!

def zeroed(): yield(0)

max([] or zeroed())
0
max([6, 66, 666] or zeroed())
666
max([] or zeroed())
0

Kidding aside, although any iterable will do such as [0] or (0,) or {0} it
does sound like it would be useful to have a silly function like the above
that makes anything such as a scalar into an iterable just to make programs
that demand an iterable happy. There probably is something out there with
some strange name like list(numb) but this way is harder for anyone
maintaining it to figure out WHY ...

def gener8r(numb): yield(numb)

max([] or gener8r(3.1415926535))
3.1415926535

But oddly although brackets work, an explicit call to list() generates an
error! Ditto for {number} working and set(number) failing. Is this an
anomaly with a meaning? 

max([] or [3.1415926535])
3.1415926535

max([] or list(3.1415926535))
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    max([] or list(3.1415926535))
TypeError: 'float' object is not iterable

max([] or list(3.1415926535, 0))
Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    max([] or list(3.1415926535, 0))
TypeError: list expected at most 1 argument, got 2

max([] or set(3.1415926535))
Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    max([] or set(3.1415926535))
TypeError: 'float' object is not iterable

max([] or {3.1415926535})
3.1415926535



-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of
Alan Gauld via Tutor
Sent: Thursday, July 7, 2022 8:33 PM
To: tutor at python.org
Subject: Re: [Tutor] this group and one liners

On 07/07/2022 23:01, avi.e.gross at gmail.com wrote:

> max([] or 0) 
> 
> breaks down with an error and the [] does not evaluate to false.

max([] or [0])


a sequence is all thats needed.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
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