Different "look and feel" of some built-in functions

Steve Keller keller.steve at gmx.de
Fri Sep 24 05:48:47 EDT 2021


Why do some built-in Python functions feel so differently:

For example sum(), all(), any() expect exactly one argument which is a
sequence to operate on, i.e. a list, an iterator or a generator etc.

    sum([1,2,3,4])
    sum(range(1, 101))
    sum(2**i for i in range(10))
    all([True, False])
    any(even, {1,2,3,4})

while other functions like set.union() and set.intersection() work on
a list of arguments but not on a sequence:

    set.intersection({1,2,3}, {3,4,5})

but

    set.union(map(...))           # does not work
    set.intersection(list(...))   # does not work

and you have to use a * instead

    set.union(*map(...))

etc.

Is this just for historical reason?  And wouldn't it be possible and
desirable to have more consistency?

Steve


More information about the Python-list mailing list