0

I was working on some code when I realized I had changed a mutli-member tuple to a single-value string but left the parens wrapped around the string, and was surprised that I hadn't encountered any problems when I realized I had left in the parens.

Simple question, why are strings inside of parens treated as strings and not tuples?

I understand this:

>>> foo= 'bar'
>>> type(foo)
<class 'str'>

And I understand this:

>>> foo = ('bar',)
>>> type(foo)
<class 'tuple'>

And this, of course:

foo = ('bar', 'baz')
>>> type(foo)
<class 'tuple'>

But I don't understand this:

>>> foo = ('bar')
>>> type(foo)
<class 'str'>

Why is ('bar') a string and not a tuple? I know adding the trailing , makes it a tuple, but for what reason was that design decision made?

LegendaryDude
  • 562
  • 8
  • 23
  • 1
    It is the **comma** that makes something a tuple. The parentheses are only there to disambiguate from other contexts where a comma might have a different meaning. – Martijn Pieters Mar 18 '16 at 20:12
  • 5
    What about `x = (4 * 5) - 3`? Would you want `(4 * 5)` to be a tuple? – zondo Mar 18 '16 at 20:12
  • @MartijnPieters You beat me to it. I was going to use this one: http://stackoverflow.com/questions/6682093/returning-tuple-with-a-single-item-from-a-function. Yours is more to the point. – Mad Physicist Mar 18 '16 at 20:13
  • You can go through this once : http://stackoverflow.com/questions/19442556/distinction-between-python-str-and-tuple-objects – Karthaveeryarjun Vinjamoori Mar 18 '16 at 20:16
  • Okay, that makes sense and I see how this is a duplicate... but what about `foo = () `? Why does that return a tuple? Seems like it should return NoneType? – LegendaryDude Mar 18 '16 at 20:21
  • @LegendaryDude: why? `()` is a valid tuple literal. – vaultah Mar 18 '16 at 20:22
  • @vaultah `()` is a valid tuple literal, `('foo')` is a string literal, and `('foo',)` is a valid tuple literal. One of these things is not like the others. I just don't quite get it -- was hoping for an explanation of why it is this way but it doesn't look like it's going to be anything other than "because." – LegendaryDude Mar 18 '16 at 20:25
  • `(None,)` would be a tuple with just `None` in it. Python doesn't just make things `None` if they aren't there. If something is `None`, that means something set it to `None`. In the case of `()`, nothing did. Since `()` doesn't really have much use besides making an empty tuple, it seems like a perfectly good way to do it. What would you have otherwise? `(,)`? – zondo Mar 18 '16 at 20:25
  • @zondo At least that would be consistent with other tuple declarations? – LegendaryDude Mar 18 '16 at 20:26
  • Doesn't it seem sort of redundant to have the comma? In the case of `(myexpression)`, the parentheses are acting as a way of containing an expression, but in the case of `()`, I don't see any use except to create an empty tuple. – zondo Mar 18 '16 at 20:28
  • @zondo I understand that, but if `()` is an empty tuple and `(1,)` is a tuple containing `1`, should not `(1)` also be a tuple containing `1`? From a certain perspective, `()` should behave the same way regardless of how it is used. – LegendaryDude Mar 18 '16 at 20:31
  • 1
    @LegendaryDude: it should, but Python uses parentheses to group expressions. – vaultah Mar 18 '16 at 20:33
  • 1
    `(1)` would be a perfectly good way to create a tuple except for the fact that parentheses are used to group expressions. To differentiate between grouping and tuple-creating, you need a comma to create a tuple. Such differentiation is not needed either for an empty tuple nor a tuple with more than one item. – zondo Mar 18 '16 at 20:35
  • @vaultah As long as I'm not the only one who thinks that logically they ought to, then it's alright with me. I understand the considerations taken to make it all work. Thanks for the insight, everyone. – LegendaryDude Mar 18 '16 at 20:37

0 Answers0