More random python observations from a perl programmer

Bernhard Herzog herzog at
Thu Aug 19 16:16:21 EDT 1999

Tom Christiansen <tchrist at> writes:

>      [courtesy cc of this posting mailed to cited author]
> In comp.lang.python, 
>     Thomas Wouters <thomas at> writes:

> :> 	>>> for c in ("weird", "bits"):
> :> 	>>> for c in ("weird"):
> :Wait, wait, you're doing two different things here. First off, you called
> :'for' with different types of arguments, the first time with a tuple of
> :strings, the second type with just a string. 
> Oh, come now, you're telling me I'm expected to count the items
> in the parens to know whether a split will happen.  Whatever. 
> It's not obvious, so it's a real gotcha.  This is not consistent.
> :But they are definately not the same :)
> But they look the same, which is wicked.
> :"Yes, the trailing comma counts" :)
> Speaking of inconsistencies!

The thing to note about tuples is that in a way it's not the parens that
make a tuple, it's the comma:

>>> t = 1, 2
>>> t
(1, 2)

The inconsistency is that () is an empty tuple, not that (1,) is a
one-element tuple. The parens are often needed because commas are also
used in list and dictionary literals and function calls and definitions,
among other places.

This is also why you can't slice dictionaries (if I understood what you
meant by slicing dicts in perl correctly). d[1,2] is equivalent to
d[(1,2)], i.e. it refers to the item with (1,2) as the key:

>>> d = {}
>>> d[1,2] = 'xyzzy'
>>> d
{(1, 2): 'xyzzy'}

Bernhard Herzog	  | Sketch, a drawing program for Unix
herzog at  |

More information about the Python-list mailing list