Python2与Python3在不同类的对象比较上的差异
来源:互联网 发布:ubuntu退出命令行界面 编辑:程序博客网 时间:2024/06/05 05:08
之前写了那么多的脚本,没有发现Python2与Python3太大的差异。接触面向对象编程,学习的过程中发现各种不同的差异。
接下来看一下Python2与Python3在不同类的对象对比上的差异。
先看一段示范代码:
class myClass:
def __init__(self,value):
self.value = value
def demo(self):
pass
myObj =myClass('abc')
myIntValue = 4
print(myObj ==myIntValue)
print(myObj >=myIntValue)
a = 'abc'
b = 123
print(a > b)
最初我用的Python2,我也期待着上面的代码在执行的时候报错。不过,执行的结果却让我觉得有意思,运行居然一切顺利!下面是运行结果:
grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/02_programme_language/03_python/03_OOP/2017/08/16$python compare_different_class.py
False
True
True
切换到Python3,运行结果如下:
E:\01_workspace\02_programme_language\03_python\03_OOP\2017\08\16>pythoncompare_different_class.py
False
Traceback (mostrecent call last):
File "compare_different_class.py",line 10, in <module>
print(myObj >= myIntValue)
TypeError: '>='not supported between instances of 'myClass' and 'int'
从上面的结果可以看出:并不支持不同类的对象之间的比较。这就有点意思了,难不成Python3比Python2的检测要严苛一些?
再次修改一下代码,依然是一个比较功能的演示:
其中,Python2中的运行结果如下:
grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/02_programme_language/03_python/03_OOP/2017/08/16$python compare_different_class.py
False
False
False
Python3中的运行结果如下:
E:\01_workspace\02_programme_language\03_python\03_OOP\2017\08\16>pythoncompare_different_class.py
False
False
False
这又是一个很有意思的现象,在比较是否相等的时候居然不同类的对象之间能够顺利比较!针对这个现象,我查了一下相关的资料。具体的解释为:如果Python遇到比较的两个类其中的一个没有实现比较方法的时候,Python会尝试交换两个比较参数。这样,只要其中的一个参数具有相应的方法实现就能够进行比较。如果两个都有实现,那么就得进行同类型的比较了。上述情况的原因其实是因为Python2中少了部分实现而导致的。下面看一下两个Python版本中类具有的方法:
其中,Python2中的信息如下:
grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/02_programme_language/03_python/03_OOP/2017/08/16$python compare_different_class.py
False
False
False
method for myClass
['__doc__','__init__', '__module__', 'demo']
method for int:
['__abs__','__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__','__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__','__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__','__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__','__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__','__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__','__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__','__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__','__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__','__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length','conjugate', 'denominator', 'imag', 'numerator', 'real']
Python3中的信息如下:
E:\01_workspace\02_programme_language\03_python\03_OOP\2017\08\16>pythoncompare_different_class.py
False
False
False
method for myClass
['__class__','__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__','__ge__', '__getattribute__', '__gt__', '__hash__', '__init__','__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__','__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__','__str__', '__subclasshook__', '__weakref__', 'demo']
method for int:
['__abs__','__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__','__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__','__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__','__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__','__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__','__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__','__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__','__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__','__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__','__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__','__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate','denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
- Python2与Python3在不同类的对象比较上的差异
- python2 与python3的print差异
- Python2与Python3中关于最上层类继承上的一点小差异
- python3 与 python2的 区别比较
- python3 与 python2的 区别比较
- Python2与Python3中__bool__方法的差异
- Python2和Python3的比较
- Python3中的print函数以及与python2的比较
- 在CentOS7上安装Python3.5.0,及其与旧版本Python2.7.5的共存问题笔记
- python3与python2的不兼容注意点小结
- tensorflow在python2和python3上的安装教程
- 关于不同类对象的覆盖问题
- 在Python2.x和Python3.x中dict.keys()的差异
- Python2与Python3的区别
- python2与python3的区别
- python2与python3的区别
- Python3与Python2的区别
- python2与python3的区别
- 拉钩求职那些困惑
- linux中的du 命令详解
- C++学习10:使用new分配内存
- Oracle扩展表空间
- 小知识点
- Python2与Python3在不同类的对象比较上的差异
- Linux下统计当前文件夹下的文件个数、目录个数
- 并查集 版子
- XAMPP下MYSQL存中文出现乱码的解决方法
- Android selinux权限
- java 猜拳游戏代码分享
- Python中的random模块
- brew安装tomcat
- C语言的指针