问题描述
我想知道在你编写条件和循环时,关键字''是'和
''不是'的规范用法是什么。我跟随的那个是完全随意的 - 我使用符号''=='',''!=''进行数值比较和/ b
是'',''不是'其他一切。
提前致谢!
这是错的。使用``==``和``!=``来测试相等/不等式和
`````和``is```进行身份测试。并且测试身份是非常罕见的。经验法则:仅用于像无这样的已知单身人士。
Ciao,
Marc''BlackJack''Rintsch
这不是随意的,你不应该使用那种惯例。
问题是真的关于平等与身份相对两个相同大小的
多维数据集可能相同,但它们肯定不是同一个多维数据集。主要的
概念:身份[通常]意味着平等,但平等几乎从不
意味着身份(两个相等的对象可能是截然不同的)。
更实际的例子,每个
数学概念具有相同度量的两个角度相等,但平行四边形上的反角不一样
one。
有时,您需要测试是否相等,例如此用户输入是否相等
到'1'? 。有时,你需要测试身份是这个用户输入
这个'1'我在这里? (注意:第二个答案应该是''不'',除非
,通过一些黑暗魔法,用户设法获得同样的''1',这是在你的
代码)。
最常见的用例是相等。你用==和!=来测试相等性。
较暗的情况是身份。您使用is测试身份。并且不是。
无论何时使用是,都要问自己一个问题:如果对象相同,否定答案是肯定的吗?
就个人而言,我喜欢使用是。与单身人士。我发现输入更容易并且
读取如果param为None比if param == None,但是一些python开发人员因为黑魔法而不喜欢第一个。参与了解
没有单身人士。
如有疑问,请测试是否平等。使用是只有当你确定你知道差价时才会知道。 Python做了一些优化,可能看起来好像
起初正在使用''是'',但实际上并非如此:
In :a =" hi"
In:b =" hi"
In:a is b
Out:True< - - 这里是黑魔法,python优化了字符串存储,
In:a是h+i < - 通过使用相同的对象而不是创建一个新对象
Out:False< - 但即使这样也不会一直有效,所以
输入:a =非常长的字符串,至少长于''hi''"
In:b =非常长的字符串,至少超过''hi''"
In:a是b
Out:False< - 永远不要依赖魔法。
我希望我有用。
干杯,
-
Luis Zarrabeitia (又名Kyrie)
Fac。 deMatemáticayComputación,UH。
I''m wondering what is the canonical usage of the keywords ''is'' and
''not'' when you''re writing conditionals and loops. The one I''ve been
following is completely arbitrary--I use the symbols ''=='', ''!='' for
numerical comparisons and the words ''is'', ''not'' for everything else.
Thanks in advance!
That''s wrong. Use ``==`` and ``!=`` for testing equality/inequality and
``is`` and ``is not`` for identity testing. And testing for identity is
quite rare. Rule of thumb: Use it only for known singletons like `None`.
Ciao,
Marc ''BlackJack'' Rintsch
It''s not arbitrary, and you shouldn''t be using that convention.
The question is really about "equality" versus "identity". Two identical sized
cubes may be equal, but they are certainly not the same cube. The main
concept here: identity [usually] implies equality, but equality almost never
implies identity (two objects that are equal may be distinct nonetheless).
More practical example, two angles with the same measure are equal by every
mathematical concept, but opposing angles on a parallelogram are not the same
one.
Sometimes, you need to test for equality, as in "is this user input equal
to ''1''?". Sometimes, you need to test for identity "is this user input
this ''1'' that I have here?" (note: the second answer should be ''no'', unless
that by some dark magic the user manages to get that same ''1'' that is in your
code).
The most common use case is equality. You test for equality with == and!=.
The darker case is identity. You test for identity with "is" and "is not".
Whenever you use "is", ask yourself a question: does a negative answer makes
sense if the objects are equal?
Personally, I like to use "is" with singletons. I find it easier to type and
read "if param is None" than "if param == None", but some python developers
dislike the first one because of the "dark magic" involved in knowing that
None is a singleton.
When in doubt, test for equality. Use "is" only when you are certain that you
know the difference. Python does some optimizations that may it seem like it
is working with ''is'' at first, but it really isn''t:
In: a="hi"
In: b="hi"
In: a is b
Out: True <-- dark magic here, python optimized the string storage,
In: a is "h"+"i" <-- by using the same object instead of creating a new one
Out: False <-- but even that won''t always work, so
In: a="a very long string, at least longer than ''hi''"
In: b="a very long string, at least longer than ''hi''"
In: a is b
Out: False <-- never, never rely on magic.
I hope I was useful.
Cheers,
--
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie
I really enjoyed the "usually" disclaimer !-)
这篇关于'是'vs'==','not vs'!='等的约定/要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!