How to create a tuple of an empty tuple in Python?

后端 未结 3 1421
旧巷少年郎
旧巷少年郎 2020-12-15 15:33

How can I create a tuple consisting of just an empty tuple, i.e. (())? I have tried tuple(tuple()), tuple(tuple(tuple())), tuple

3条回答
  •  自闭症患者
    2020-12-15 15:39

    The empty tuple is () (or the more-verbose and slower tuple()), and a tuple with just one item (such as the integer 1), called a singleton (see here and here) is (1,). Therefore, the tuple containing only the empty tuple is

    ((),)
    

    Here are some results showing that works:

    >>> a=((),)
    >>> type(a)
    
    >>> len(a)
    1
    >>> a[0]
    ()
    >>> type(a[0])
    
    >>> len(a[0])
    0
    

提交回复
热议问题