Why single element tuple is interpreted as that element in python?

前端 未结 5 1522
[愿得一人]
[愿得一人] 2020-12-04 02:44

Could anyone explain why single element tuple is interpreted as that element in Python?

And

Why don\'t they just print the tuple (1,) as (

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 03:21

    Because only (1, ) in your examples is tuple. The rest are expressions.

    In [4]: type(1,)
    Out[4]: int
    
    In [5]: type((1,))
    Out[5]: tuple
    
    In [6]: type((1))
    Out[6]: int
    

提交回复
热议问题