why does the % string format operator only take tuples?

Kragen Sitaker kragen at dnaco.net
Tue Apr 3 20:28:36 EDT 2001


In article <gha66glj2n4.fsf at synopsys.com>,
Ram Bhamidipaty  <ramb at synopsys.com> wrote:
>Does anyone else think it would be a good idea of the % operator
>were extended to also handle lists?

No.  Lists have the connotation that they could have any number of
elements and that there isn't much distinction between the elements
(some come earlier, some later, but they're all the same kind of thing
in some sense --- any code you could pass foo[0] to should also be able
to handle foo[1] if it's there).  Tuples have the connotation that
their elements have distinct meanings.

Obviously, sequences that are the right operand of string % must have
exactly the right number of elements, and their elements are likely to
have different meanings (otherwise you'd use a loop instead of passing
them to the same %).

These are stylistic matters, of course; Python will let you use your
tuples as immutable lists and your lists as mutable tuples.  It's
probably bad style to do this, though.

The right argument of % can also be a dictionary.
-- 
<kragen at pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
Perilous to all of us are the devices of an art deeper than we possess
ourselves.
       -- Gandalf the White [J.R.R. Tolkien, "The Two Towers", Bk 3, Ch. XI]




More information about the Python-list mailing list