Understanding Python's “is” operator

前端 未结 11 2275
别那么骄傲
别那么骄傲 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 22:59

    It compares object identity, that is, whether the variables refer to the same object in memory. It's like the == in Java or C (when comparing pointers).

    0 讨论(0)
  • 2020-11-21 23:01

    X points to an array, Y points to a different array. Those arrays are identical, but the is operator will look at those pointers, which are not identical.

    0 讨论(0)
  • 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]
    
    0 讨论(0)
  • 2020-11-21 23:05

    x is y is same as id(x) == id(y), comparing identity of objects.

    As @tomasz-kurgan pointed out in the comment below is operator behaves unusually with certain objects.

    E.g.

    >>> class A(object):
    ...   def foo(self):
    ...     pass
    ... 
    >>> a = A()
    >>> a.foo is a.foo
    False
    >>> id(a.foo) == id(a.foo)
    True
    

    Ref;
    https://docs.python.org/2/reference/expressions.html#is-not
    https://docs.python.org/2/reference/expressions.html#id24

    0 讨论(0)
  • 2020-11-21 23:06

    A simple example with fruits

    fruitlist = [" apple ", " banana ", " cherry ", " durian "]
    newfruitlist = fruitlist
    verynewfruitlist = fruitlist [:]
    print ( fruitlist is newfruitlist )
    print ( fruitlist is verynewfruitlist )
    print ( newfruitlist is verynewfruitlist )
    

    Output:

    True
    False
    False
    

    If you try

    fruitlist = [" apple ", " banana ", " cherry ", " durian "]
    newfruitlist = fruitlist
    verynewfruitlist = fruitlist [:]
    print ( fruitlist == newfruitlist )
    print ( fruitlist == verynewfruitlist )
    print ( newfruitlist == verynewfruitlist )
    

    The output is different:

    True
    True
    True
    

    That's because the == operator compares just the content of the variable. To compare the identities of 2 variable use the is operator

    To print the identification number:

    print ( id( variable ) )
    
    0 讨论(0)
  • 2020-11-21 23:08

    Prompted by a duplicate question, this analogy might work:

    # - Darling, I want some pudding!
    # - There is some in the fridge.
    
    pudding_to_eat = fridge_pudding
    pudding_to_eat is fridge_pudding
    # => True
    
    # - Honey, what's with all the dirty dishes?
    # - I wanted to eat pudding so I made some. Sorry about the mess, Darling.
    # - But there was already some in the fridge.
    
    pudding_to_eat = make_pudding(ingredients)
    pudding_to_eat is fridge_pudding
    # => False
    
    0 讨论(0)
提交回复
热议问题