Python Pycharm:预期类型“Integral”,改为“str”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23962434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pycharm: Expected type 'Integral', got 'str' instead
提问by avalanchy
I just installed PyCharm 3.4 and get some new warnings. Not just here but in many places. Code is fine of course. Can someone translate what PyCharm trying to tell me and how to silence this messages?
我刚刚安装了 PyCharm 3.4 并收到了一些新警告。不只是这里,很多地方都是如此。代码当然没问题。有人可以翻译 PyCharm 试图告诉我的内容以及如何使这些消息静音吗?
采纳答案by David Pope
Based on the 'more...' screenshot, it looks like Pycharm might be interpreting the map() as though the two terms around the comma are both part of the lambda, i.e. the lambda just returns a 2-tuple instead of treating it as two parameters to the map() function.
基于“更多...”屏幕截图,看起来 Pycharm 可能正在解释 map() 好像逗号周围的两个术语都是 lambda 的一部分,即 lambda 只返回一个 2 元组而不是处理它作为 map() 函数的两个参数。
Things to try:
尝试的事情:
- Add parentheses inside the map()
- look for redefinitions of the map() builtin itself that might be confusing Pycharm
- 在 map() 内添加括号
- 寻找可能混淆 Pycharm 的 map() 内置本身的重新定义
EDIT
编辑
You inspired me to go learn more about Python and Pycharm. :)
你激励我去学习更多关于 Python 和 Pycharm 的知识。:)
It looks like Pycharm is happier with using a list comprehension than with map()
. Using this sample data:
看起来 Pycharm 更喜欢使用列表理解而不是map()
. 使用此示例数据:
data = {
'data': {
'children': [
{'data': {'url': 'http://1.com/', }, },
{'data': {'url': 'http://2.com/', }, },
]
},
}
if you write the code like you did, then you get the error:
如果您像以前一样编写代码,则会收到错误消息:
items = map(lambda children: children['data'], data['data']['children'])
for item in items:
print item['url'] # Pycharm shows warning on 'url'
But if you use a list comprehension instead, then Pycharm is happy:
但是如果你改用列表推导式,那么 Pycharm 会很高兴:
items = [x['data'] for x in data['data']['children']]
for item in items:
print item['url'] # No warning from Pycharm
And the output is the same for both.
两者的输出相同。
ISTR reading that list comprehensions are preferred over map()
nowadays anyway, so maybe Pycharm is nudging us in that direction?
map()
无论如何,ISTR 阅读列表理解比现在更受欢迎,所以也许 Pycharm 正在朝这个方向推动我们?
回答by t_io
You can also get rid of this notification if you get the value from the dictionary with get()
. This should work:
fresh_urls = {item.get('url') for item in items}
如果您从字典中获取值,您也可以摆脱此通知get()
。这应该有效:
fresh_urls = {item.get('url') for item in items}