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

Steve Keller keller.steve at gmx.de
Fri Sep 24 18:15:10 EDT 2021


"Dieter Maurer" <dieter at handshake.de> writes:

> Steve Keller wrote at 2021-9-24 11:48 +0200:
> >Why do some built-in Python functions feel so differently:
> 
> Because the typical use cases are different
> 
> [...]
> 
> >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})
> 
> Those operations are typically applied to a small number
> of operands. You would need to build an iterator in that
> case should the functions only accept iterators.

In my experience I need intersection and union on a list of sets, set
of sets or a map() returning sets much more often.  E.g. in some
mathematical problems, and in automaton theory (IIRC, computing of LR
or LALR sets, converting NFA to DFA, minimizing DFA), many graph
algorithms traversing the nodes (shortest path, ...), and so on).

Intersection and union of two sets is actually often seen in naïve
programming in loops like

    for t in (some_sequence):
        s.union(t)

where set.union(*(some_sequence)) would be much more elegant.

BTW, I like how the min() and max() functions allow both ways of being
called.

Steve


More information about the Python-list mailing list