AttributeError:“NoneType”对象没有属性“userName”

2024-10-02 20:35:08 发布

您现在位置:Python中文网/ 问答频道 /正文

reqUser =  friends.UserFriends.gql("where udid =:1", str(udid) ).get()
reqUserName = reqUser.userName

这是我的代码,我想知道如何处理这个错误,这个错误出现在第2行


Tags: 代码get错误usernamewheregqlfriendsstr
1条回答
网友
1楼 · 发布于 2024-10-02 20:35:08

你可以使用pythons try/except pair。在

reqUser =  friends.UserFriends.gql("where udid =:1", str(udid) ).get()
try:
    reqUserName = reqUser.userName
except AttributeError:
    reqUserName = "No username found!"

这只捕获AttributeError异常,因此仍将引发任何其他异常。在

相关问题 更多 >