Python 中的比较方法 is与==的比较
来源:互联网 发布:2016流行网络语 编辑:程序博客网 时间:2024/06/08 07:01
Example:
>>> test4='this is hello world'
>>> test4[8:13]'hello'
>>> test4[8:13] is 'hello'
False
>>> test4[8:13] == 'hello'
True
例子中is和==都是比较的方法,但'==' 是用来比较两个对象的值是否相同( equality test),类似 'hello'.__eq__(test4[8:13])
但is比较方法是比较两个对象是不是同一个对象( identity test),相当于:
id(test4[8:13]) == id('hello')
而且这里是比较字符串,字符串有个 interned 特性(string interning is a method of storing only one copy of each distinct string value),详情见:
http://en.wikipedia.org/wiki/String_interning
所以当比较非字符串时:
>>> [1, 2] == [1,2]
True
>>> [1, 2] is [1,2]
False
参考资料:
http://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce
http://blog.csdn.net/sasoritattoo/article/details/12451359
http://en.wikipedia.org/wiki/String_interning
- Python 中的比较方法 is与==的比较
- Python解惑:整数比较 is ==的比较
- python中的is、==和cmp()比较字符串
- Python中的比较运算符,要特别注意的是 == 和 is 的区别
- += 方法 与 concat 方法的比较
- python 中is和= = 的比较
- python对象的比较(is,==,对象大小比较>,<)
- ==与equals方法的比较
- IS-IS与OSPF的比较
- 比较IS-IS与OSPF的区别
- python 中赋值 copy() 与 ' = ' 号以及比较符'=='号 与 'is'
- java中string对象的”equal“方法和“==”运算符 与 c#中的比较
- Java中的equals方法和比较运算符”==“的使用和比较
- Python与Ruby的比较
- PHP与Python的比较
- PHP与Python的比较
- python与java的 比较
- Perl与Python的比较
- Use the SQL Server CLR to Read and Write Text Files
- Unity3D脚本18:可视化辅助设置类 Gizmos
- ListView与Adapter使用要点
- System.Data.OracleClient.OracleConnection的类型初始化值设定项引发异常
- void RCC_Configuration(void)
- Python 中的比较方法 is与==的比较
- awk 正则表达式、正则运算符详细介绍
- Android资源文件简介
- 关于用vc来操作usb设备
- 动态树LCT总结
- Android 结束进程的方法
- 久坐会以何种方式、何种程度伤害你的身体?
- 交换两个数值型变量的最快方法
- HDU 4967 Handling the Past