Why can't I add a tuple to a list with the '+' operator in Python?

前端 未结 3 1226
慢半拍i
慢半拍i 2020-12-20 23:09

Python not support adding a tuple to a list:

>>> [1,2,3] + (4,5,6)
Traceback (most recent call last):
  File \"\", line 1, in 

        
3条回答
  •  长情又很酷
    2020-12-20 23:23

    This is not supported because the + operator is supposed to be symmetric. What return type would you expect? The Python Zen includes the rule

    In the face of ambiguity, refuse the temptation to guess.
    

    The following works, though:

    a = [1, 2, 3]
    a += (4, 5, 6)
    

    There is no ambiguity what type to use here.

提交回复
热议问题