我可以理解,某些语言允许用户执行一些操作符重载。我首先在C ++领域知道这一点。但是c ++对运算符重载也有一些限制,我认为这是合理的。
但是当我来到python pandams库时。我开始感到困惑。
看一下我在nbviewer.jupyter.org上的代码
complaints['Complaint Type'] == "Noise - Street/Sidewalk"
不返回True或False。
这对我来说太疯狂了。有人可以帮助我了解这一点吗?
从链接复制了一些相关结果:
>>> complaints['Complaint Type'] == "Noise - Street/Sidewalk"
0 True
1 False
2 False
3 False
4 False
...
111063 False
111064 False
111065 False
111066 True
111067 False
111068 False
Name: Complaint Type, Length: 111069, dtype: bool
如果创建自己的类并向其添加__eq__
方法,则可以重载运算符。
class MyClass(object):
def __eq__(self, other):
# compare self with other, return whatever you need
每当您将类型与进行比较时,都会调用此方法self == other
。从python中的此函数返回布尔值被认为是非常正常的,因此,如果您希望对其他开发人员有意义的代码,则可能需要考虑返回其他任何内容。
在这里查看python 2的文档
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句