length of a tuple or a list containing only one element

Arnaud Delobelle arnodel at googlemail.com
Mon Nov 3 12:20:33 EST 2008


bearophileHUGS at lycos.com writes:

> Steve Holden:
>> While this kind of beginner
>> mistake is common it isn't one that's frequently repeated once the
>> learner understands the syntax.
>
> You may be right, but I don't have to like it.
> When you teach programming to people that have never done it before,
> and you use Python, they spot similar inconsistences in the blink of
> an eye and often look annoyed. [...]

You can teach them that the comma is a terminator rather than a
separator (no more inconsistencies), but that the python parser is
forgiving and understands when the last comma is missing if the
expression is not already meaningful.

I.e. one should write

    (1, 2, 3,)

But python, being nice, understands when you write

    (1, 2, 3)

Now consider this:

    3 * (1 + 2)

What interpretations should python take of the brackets?

> The good thing is that Python3 fixes some of those things :-)

And introduces some new inconsistencies for newcomers, e.g.

s = {1, 2, 3} # A set with 3 elements
s = {1} # A set with one element
s = {} # Surely, this should be an empty set!!

-- 
Arnaud

-- 
Arnaud



More information about the Python-list mailing list