[Python-Dev] syntactic support for sets

Raymond Hettinger raymond.hettinger at verizon.net
Wed Feb 1 23:49:21 CET 2006

[Greg Wilson]
> This is a moderately-fertile source of bugs for newcomers: judging from
> the number of students who come into my office with code that they think
> ought to work, but doesn't, most people believe that:
>    set(1, 2, 3)

Like many things in Python where people pre-emptively believe one thing
or another, the interpreter's corrective feedback is immediate:

    >>> set(1, 2, 3)
    Traceback (most recent call last):
        set(1, 2, 3)
    TypeError: set expected at most 1 arguments, got 3

There is futher feedback in the repr string which serves as a reminder
of how to construct a literal:

    >>> set(xrange(3))
    set([0, 1, 2])

Once the students have progressed beyond academic finger drills and
have started writing real code, have you observed a shift in emphasis
away from hard-coded literals and towards something like s=set(data)
where the data is either read-in from outside the script or generated by
another part of the program?

For academic purposes, I think the genexp form also has value in that
it is broadly applicable to more than just sets (i.e. dict comprehensions)
and that it doesn't have to grapple with arbitrary choices about whether
{1,2,3} would be a set or frozenset.


More information about the Python-Dev mailing list