[python 2.4] unable to construct tuple with one item

Mel Wilson mwilson at the-wire.com
Sun May 6 11:04:11 EDT 2007


Vyacheslav Maslov wrote:
> So, the main question is why using syntax like [X] python constuct list 
> with
> one item, but when i try to construct tuple with one item using similar
> syntax (X) python do nothing?

Because `(` and  `)` are used in expressions to bracket sub-expressions.

a = (4 + 3) * 2

means: add 4 to 3, multiply the sum by 2.  It can't mean: make a tuple 
containing 7 and extend it to 7,7 .

The real constructor for tuples is `,`.  When you see parens around 
tuple creation they're there to bracket the sub-expression that 
creates the tuple.  You see new tuples without parens all the time:

for i, thing in enumerate (things): ...

starter, main_course, dessert, beverage = order_meal (*hunger)

etc.


	Mel.




More information about the Python-list mailing list