python - 验证集合时调用的方法

标签 python

假设我有一个列表Q。在下面的代码中:

while Q:
    do_something()
    Q.pop()

while Q语句中,调用了列表Q的什么方法?是__len__方法吗?

最佳答案

在 Python 3.x 中,它是 __bool____len__:

object.__bool__(self)

Called to implement truth value testing and the built-in operation bool(); should return False or True. When this method is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither __len__() nor __bool__(), all its instances are considered true.

在 2.x 中它被命名为 __nonzero__ ;请参阅what's new in 3.0 .

关于python - 验证集合时调用的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43708561/

相关文章:

python - Tkinter:如何通过函数将两个命令分配给一个按钮?

python - 在 matplotlib 中绘制黑白二值图

python - 如何简单地从该图书价格数据集中提取版本类型、月份和年份?

python - 在python数据帧中的每列的最大值之前找到一个值的索引

python - Pandas read_csv 更改以 0 开头的列

javascript - 在 Python 中将 base64 数据解码为数组

python - Matplotlib:从条形图中获取颜色和 x/y 数据

python - 如何将 pyspark.sql.dataframe.DataFrame 转换回 databricks notebook 中的 sql 表

python - 如何在 Linux 中获取包含感兴趣的特定文件的最新文件夹并在 Python 中使用 Paramiko 下载该文件?

python - 从 python 中的 c 模块捕获打印输出