Python 2.x gotchas and landmines

后端 未结 23 2310
北恋
北恋 2020-11-28 17:47

The purpose of my question is to strengthen my knowledge base with Python and get a better picture of it, which includes knowing its faults and surprises. To keep things sp

23条回答
  •  暖寄归人
    2020-11-28 18:05

    x += [...] is not the same as x = x + [...] when x is a list`

    >>> x = y = [1,2,3]
    >>> x = x + [4]
    >>> x == y
    False
    
    >>> x = y = [1,2,3]
    >>> x += [4]
    >>> x == y
    True
    

    One creates a new list while the other modifies in place

提交回复
热议问题