Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is 和 ==的使用 #2

Closed
FlyaOoo opened this issue Nov 1, 2018 · 2 comments
Closed

is 和 ==的使用 #2

FlyaOoo opened this issue Nov 1, 2018 · 2 comments

Comments

@FlyaOoo
Copy link

FlyaOoo commented Nov 1, 2018

最佳实践的第二小节别在字符串上走太远,改进后的代码中使用了 !=None ,虽然在这里没有什么大问题,但是既然主题是匠心,还是忍不住说了,😂。另外,对于字符串的比较是用is还是==想听听大神的意见。PS,我明白is和==的区别,却不是很明白该怎么更精确的选择使用,就比如这里的!=None也没有什么问题,想听听关于字符串,甚至内置其他内置数据类型的比较是用is还是==的一个详细解释

@piglei
Copy link
Owner

piglei commented Nov 1, 2018

这。。。我跟你说,这一定是一个由于地球磁场不稳定产生的 bug,哈哈哈哈。

关于 is 和 ==,我觉得还是得严格限制对于 is 的使用,对于那些我们明确知道在 Python 里只有单例的对象才使用 is 来判断完全一致性,比如 None, True, False,以及你定义的任何单例对象。假如我说,我们比起 == 更推荐用 is 的话,会产生比起全部用 == 来说更严重的问题,非常困扰新人:

>>> a = 10987
>>> a is 10987
False

>>> a = 256
>>> a is 256
True

@piglei piglei closed this as completed in 8316e1a Nov 1, 2018
@piglei
Copy link
Owner

piglei commented Nov 1, 2018

谢谢提醒,已经调整文章内容。

piglei pushed a commit that referenced this issue Jun 26, 2019
Update 1-using-variables-well.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants