Understanding Python's “is” operator

前端 未结 11 2282
别那么骄傲
别那么骄傲 2020-11-21 22:42

The is operator does not match the values of the variables, but the instances themselves.

What does it really mean?

11条回答
  •  梦如初夏
    2020-11-21 23:03

    is only returns true if they're actually the same object. If they were the same, a change to one would also show up in the other. Here's an example of the difference.

    >>> x = [1, 2, 3]
    >>> y = [1, 2, 3]
    >>> print x is y
    False
    >>> z = y
    >>> print y is z
    True
    >>> print x is z
    False
    >>> y[0] = 5
    >>> print z
    [5, 2, 3]
    

提交回复
热议问题