[Tutor] this group and one liners

avi.e.gross at gmail.com avi.e.gross at gmail.com
Fri Jul 8 12:54:02 EDT 2022


Alan,

That shakes me up a bit as I do not recall ever needing to use iterables in
the context of set() or list() back when I was learning the language. Then
again, I mainly would use other notations or convert to that data type from
another.

But this can be a teaching moment.

Part of the assumption is based o my experience with quite a few other
languages and each has its own rules and even peculiarities. In some
languages, you use list(0 to create a list and something like as.list() to
coerce another object not a list.

Further confusing issues is that sets and lists use a special notation of []
or {} to often handle sets. So, it seems that 
	var = [5]

And 

	var = list(5)

are far from the same thing.

So it seems one way to make a list is to use list() with no arguments to
make an empty list, or just use [] and then use list methods on the object
to append or insert or extend. But there is no trivial way to do that inside
the argument list of max() as described.

Similarly for set() which either makes an empty set (the only way to do that
as {} makes an empty dictionary) or coerces some iterable argument into a
set. 

I am not convinced this is fantastic design. The concept of an iterable is
very powerful but this brings us back to the question we began with. Why not
have a version of max() that accepts numbers like 1,2,3 as individual
arguments?

In my view, just like a nonexistent stretch of zeros in a string is
considered to be of length zero for our purposes, not unknown, I can imagine
many scenarios where a single value should be useful in the context of many
functions as if it was an iterable that returned one value just once. Heck,
I can see scenarios where a null value that returned nothing would be an
iterable. Python allows you to build your own iterables in several ways that
do exactly that!

So, yes, you can get around these rules when needed but outside of
one-liners, that may rarely be an issue if you know the rules.

Every language has plusses and minuses from the perspective of a user and
you either deal with it or try to use another. But if people seem to want to
do things a certain way, the language often is added to in ways  that force
things to be doable, albeit with more work and since [3.14] is enough to
make the change, perhaps not necessary. Still for a flexible dynamic
language, this way of doing things looks like a throwback to me. Many
programming paradigms are like that. They are great when used as designed
and a source of lots of frustration when not.








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

On 08/07/2022 02:52, avi.e.gross at gmail.com wrote:

> 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?

list() and set() require iterables. They won't work with single values:

>>> set(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> set((1,))
{1}
>>> list(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> list([3])
[3]
>>> list((3,))
[3]
>>>

The error you are getting is not from max() its from list()/set()

What is slightly annoying is that, unlike max(), you cannot just pass a
sequence of values:

>>> set(1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: set expected at most 1 argument, got 2


--
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