Create a single element tuple of tuple

五迷三道 提交于 2019-12-20 03:33:18

问题


I just noticed that if you want to create a tuple with single element, being a tuple itself, you cannot do it with constructor tuple only with (,) syntax. Why is that?

Example:

>>> tuple(list('abc'))
('a', 'b', 'c')
>>> tuple(tuple(list('abc')))
('a', 'b', 'c')
>>> (tuple(list('abc')),)
(('a', 'b', 'c'),)

But then it holds for a list

>>> tuple([1],)
(1,)
>>> tuple([1])
(1,)

回答1:


I don't really see the issue, this adheres to the documentation:

class tuple(object)
 |  tuple() -> empty tuple
 |  tuple(iterable) -> tuple initialized from iterable's items
 |  
 |  If the argument is a tuple, the return value is the same object.

So, list('abc') always evaluates to ['a', 'b', 'c'] which is an iterable.

So in the first example (tuple(['a', 'b', 'c'])), the result is a tuple initialised from the iterable's items. I.e. ('a', 'b', 'c').

The second example takes the result of the first example (a tuple) and passes it into the tuple() function once more. As the documentation states (last line), the return value when passed a tuple is the same object which matches with our result.

And for the third, once more, the docs tell us what we need to know:

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).


Finally, your last two examples (tuple([1]) and tuple([1],)) both produce a one-element tuple, because you are passing an iterable of length one. The docs once again state (at top): a tuple is initialized from iterable's items.

So, to conclude, the case where you need a comma is when you want to create a tuple with one element. However, if passing an iterable of length one, this is not necessary as Python understands that you are not evaluating an expression.


For completeness, the reason this awkward syntax is unavoidable is because statements like: (1 + 2) * 3 would evaluate to (3, 3, 3) rather than the expected 9. So instead, you must go out of your way through adding a comma: (1 + 2,) * 3 to get the result of (3, 3, 3) which makes perfect sense.




回答2:


Without a trailing comma, Python will treat the value which you are passing as a expression.

>>> tuple(tuple(list('abc')))

will give you

tuple(('a','b','c'))

Which is then evaluated into a tuple

('a','b','c')

Here is some more info if you need

As for the list, tuple() takes iterables to create a tuple




回答3:


its just the way it is.

Calling a constructor on an existing object does nothing (see list(list('1')) for example. Calling tuple(list('1')) would have to create a tuple, hence the ,. This is not required from your 2nd call (tuple(tuple(list('abc')))) where the inner tuple already exists, and therefore, "casting" it to a tuple does nothing




回答4:


... you cannot do it with constructor tuple only with (,) syntax

Yes you can. Simply you must add a comma when building a tuple containing one single element, because if you do not, you only get a parenthesed non tuple element:

t = ((1,),)

is a tuple containing one single tuple containing integer 1.

But tuple only converts an iterable to a tuple. As such, it changes a string in a tuple of chars and changes a list in tuple. And if you pass a tuple, you just get a copy of the original tuple.



来源:https://stackoverflow.com/questions/51242063/create-a-single-element-tuple-of-tuple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!