如果我不尝试迭代,为什么我得到“类型'bool'不可迭代”?

时间:2022-07-09 16:35:34

Could someone with superior python skills please explain why am I getting a "bool is not iterable" error, when I am only doing some dict lookups and comparisons?

有没有优秀python技能的人可以解释为什么我得到一个“bool不可迭代”错误,当我只做一些字典查找和比较?

This is the last line of the stack trace:

这是堆栈跟踪的最后一行:

---> 54     if 'identifiers' in document and 'doi' in document['identifiers'] and document['identifiers']['doi'] == row['doi']:
     55         print 'Found DOI'
     56         return True

TypeError: ("argument of type 'bool' is not iterable", u'occurred at index 2914')

Is best practice to use a try/except to attempt to read my dict and catch if the key does not exist?

最佳做法是使用try / except尝试读取我的dict并捕获密钥是否不存在?

3 个解决方案

#1


8  

When an object doesn't provide contains(), in attempts to iterate it (https://docs.python.org/2/reference/expressions.html#membership-test-details)

当一个对象没有提供contains()时,试图迭代它(https://docs.python.org/2/reference/expressions.html#membership-test-details)

>>> print 1 in True
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
>>> 

The problem seems to be that the key does exist, but has the wrong type, so the in check isn't helpful here. Generally, in python it's better to follow EAFP and handle an exception instead of trying to pre-check everything.

问题似乎是密钥确实存在,但是类型错误,所以检查在这里没有用。通常,在python中,最好遵循EAFP并处理异常,而不是尝试预先检查所有内容。

#2


1  

document['identifiers'] is a bool type, and the 'in' is trying to iterate over a bool variable which is giving you this error.

document ['identifiers']是一个bool类型,'in'试图迭代一个bool变量,它给你这个错误。

#3


-1  

I don't think this is the cause of your error, but you can use the get method:

我不认为这是您的错误的原因,但您可以使用get方法:

dict.get(key, default=None)

dict.get(key,default = None)

You can define an optional default value to return if the key is not present, and it won't trip an error.

如果密钥不存在,您可以定义一个可选的默认值,并且不会触发错误。

#1


8  

When an object doesn't provide contains(), in attempts to iterate it (https://docs.python.org/2/reference/expressions.html#membership-test-details)

当一个对象没有提供contains()时,试图迭代它(https://docs.python.org/2/reference/expressions.html#membership-test-details)

>>> print 1 in True
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
>>> 

The problem seems to be that the key does exist, but has the wrong type, so the in check isn't helpful here. Generally, in python it's better to follow EAFP and handle an exception instead of trying to pre-check everything.

问题似乎是密钥确实存在,但是类型错误,所以检查在这里没有用。通常,在python中,最好遵循EAFP并处理异常,而不是尝试预先检查所有内容。

#2


1  

document['identifiers'] is a bool type, and the 'in' is trying to iterate over a bool variable which is giving you this error.

document ['identifiers']是一个bool类型,'in'试图迭代一个bool变量,它给你这个错误。

#3


-1  

I don't think this is the cause of your error, but you can use the get method:

我不认为这是您的错误的原因,但您可以使用get方法:

dict.get(key, default=None)

dict.get(key,default = None)

You can define an optional default value to return if the key is not present, and it won't trip an error.

如果密钥不存在,您可以定义一个可选的默认值,并且不会触发错误。