python中is的用法,什么时候(如果有的话)在Python中使用'is'关键字?

Today I learnt about the is keyword in Python and tried the following:

>>> x=2+2

>>> y=4

>>> x is y

True

I started off trying is with integers because I knew the answer would be False -- so I found the result very surprising! To give some context, my background is C++ and C# where there is a distinction between value types and object types. In Python, as I now realize, everything is a reference type.

It seems the reason that x is y is True is the same as explained in this question, How is the 'is' keyword implemented in Python?, which relates to using is with strings. I.e. the run-time environment saves memory by sharing, or "interning" integers just as it does with strings -- this is explained in more detail in answers to a question: Python “is” operator behaves unexpectedly with integers I found after my initial post.

Another thing I find surprising is that the value that is returns is implementation dependent. Which relates to my main question. In the referenced question about the implementation of is w.r.t strings, there was some discussion about when to use is, with several users saying they would (almost) never use it. So my question is, when should the is keyword be used? What are some canonical examples, or general rules?

解决方案

You should use is when you want to know whether two objects are the same object. Don't use it when you want to know whether two objects have the same value.

There is a canonical example, but it is unfortunately not very helpful. People will tell you to always test for the None value using x is None instead of x == None. However, there is little practical difference between these cases. (See this question for explanation.)

In some situations, you may wind up creating objects which have the same value but are distinct objects. For instance, you could imagine creating a fantasy wargame in which the player can magically create minions to battle his opponent. So the player might create 100 identical orcs or whatever. Each orc could be represented by an object, and they would be identical in that they have the same properties, but still distinct in that there would be 100 separate objects. Now if the opponent tries to cast a "fireball" spell on one of these orcs, while on the same turn the player tries to cast "protect against fire" on an orc, you might want to check if the target of the fireball spell is the target of the protection spell. Equality wouldn't be enough, because all the orcs are equal, but only one particular orc is the target of each spell, and you want to know if the two targets are the same object. This is a rather contrived example, but should give a broad idea of the kind of situation where you might wind up using is.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值