Understanding Python's “is” operator

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

提交回复
热议问题