绕过列表理解中的非类型
要在没有值x.competition.name
'competition': [x.competition.name if x.competition.name is not None else '-' for x in a]
情况
AttributeError: 'NoneType' object has no attribute 'name'
的 围绕这个问题?
To work around when there is no value x.competition.name
, I tried to use is not None
:
'competition': [x.competition.name if x.competition.name is not None else '-' for x in a]
But the error still keeps showing up:
AttributeError: 'NoneType' object has no attribute 'name'
How can I go about getting around this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您需要测试
x.competition
:'竞赛':[x.competition.name如果x.com.petition不是没有其他' - >
It sounds like you need to test
x.competition
:'competition': [x.competition.name if x.competition is not None else '-' for x in a]
显然没有竞争,所以请
使用
Apparently competition is None, so please replace
using
错误消息说您尝试获得
none.name
。这意味着x.competition
必须是无
,这就是为什么您无法从中获得name
属性的原因。相反,尝试使您的条件x.com.petition不是无
。The error message is saying that you tried to get
None.name
. That meansx.competition
must beNone
, which is why you can't get aname
attribute from it. Instead, try making your conditionx.competition is not None
.