one-element tuples [Was: Most probably a stupid question, but I still want to ask]

Stephen Hansen me+python at ixokai.io
Sun Apr 10 20:18:31 EDT 2016


On Sun, Apr 10, 2016, at 05:13 PM, Fillmore wrote:
> I guess that the answer to my question is: there is no such thing as a
> one-element tuple,
> and Python will automatically convert a one-element tuple to a string...
> hence the
> behavior I observed is explained...
> 
>  >>> a = ('hello','bonjour')
>  >>> b = ('hello')
>  >>> b
> 'hello'
>  >>> a
> ('hello', 'bonjour')
>  >>>
> 
> 
> Did I get this right this time?

No, you didn't. Your mistake is again -- parens don't make tuples,
commas do.

A one element tuple is:
>>> b = ("hello,)

The parens are optional, I always put them in because:
>>> b = "hello",

The parens group an expression, they don't make a type.

--
Stephen Hansen
  m e @ i x o k a i . i o



More information about the Python-list mailing list