python - 和python中的操作重载

标签 python

我知道逻辑上 and 用于 bool 值,如果两个条件都为真,则评估为真,但我对以下语句有疑问:

print "ashish" and "sahil"

it prints out "sahil"?
 another example:
 return s[0] == s[-1] and checker(s[1:-1])
 (taken from recursive function for palindrome string
 checking            
please explain it and other ways and is oveloaded ,especially what the second statement do.

最佳答案

and 没有重载。

在您的代码中,"ashish" 是一个真值(因为非空字符串是真值),因此它计算 "sahil"。由于 "sahil" 也是一个真值,因此 "sahil" 返回到 print 语句然后被打印。

关于python - 和python中的操作重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18644263/

相关文章:

python - 用 Python 替换字符串的子字符串

python - 迪维希语 django-pisa pdf 的问题

python - 如果行的内容包含特定的子字符串,如何排除数据框中的列?

python - 规范化遗漏波兰语字符

python - 在 Python 中分析内存分配(支持 Numpy 数组)

python - 将基本变量列表写入文本文件

python - Pandas:将新列插入数据框并根据 if then 逻辑在新列中填充值

python - 函数名作为另一个函数的输入?

python - Djangocollectstatics仅收集管理静态文件

python - 如何将多列乘以另一列 Pandas