首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >same_row = self.row == other.row AttributeError:'NoneType‘对象没有属性'row’与gspread

same_row = self.row == other.row AttributeError:'NoneType‘对象没有属性'row’与gspread
EN

Stack Overflow用户
提问于 2022-09-30 16:00:24
回答 1查看 189关注 0票数 0

我试图编程一个不和谐的机器人,链接到谷歌页,但已经遇到了无数的错误,我的代码。我得到的错误如下:

代码语言:javascript
复制
Traceback (most recent call last):
  File "/home/runner/Bundeswehr-Bot/venv/lib/python3.8/site-packages/discord/ui/modal.py", line 186, in _scheduled_task
    await self.on_submit(interaction)
  File "/home/runner/Bundeswehr-Bot/modals.py", line 45, in on_submit
    if sheet.find(self.host.value) == None:
  File "/home/runner/Bundeswehr-Bot/venv/lib/python3.8/site-packages/gspread/cell.py", line 44, in __eq__
    same_row = self.row == other.row
AttributeError: 'NoneType' object has no attribute 'row'

我的代码是:

代码语言:javascript
复制
class bct_modal(discord.ui.Modal, title="BCT Logging"):
  host = discord.ui.TextInput(
        label='Host ID',
        placeholder='The hosts discord ID here...',
        required=True
    )

  async def on_submit(self, interaction: discord.Interaction):
      ids = []
      ids.append(self.host.value)

      print(ids)
      if sheet.find(self.host.value) == None:
        sheet.append_row([self.host.value, 0, 0, 0, 0, 0, 0, 0, 0])
        for id in ids:
          bct_host_col = sheet.find("Hosted BCT").col
          cell_row = sheet.find(id).row
          sheet.update_cell(cell_row, bct_host_col, int(sheet.cell(cell_row, bct_host_col).value) + 1)
      elif sheet.find(self.host.value) != None:
        print("This ID is already in the sheet.")

任何帮助都将不胜感激。

注意:当我输入一个还没有在google电子表格中的ID时,它会按照我想要的方式完美地附加它,但是如果我尝试输入一个已经在电子表格中的ID,它会抛出这个错误。

EN

回答 1

Stack Overflow用户

发布于 2022-10-24 01:41:09

在您的脚本中,我认为可能需要修改if sheet.find(self.host.value) == None:。所以,==is。我认为这可能是您发布'NoneType' object has no attribute 'row'的原因。

我认为在这种情况下,this thread可能是有用的。

那么,下面的修改如何?

发自:

代码语言:javascript
复制
if sheet.find(self.host.value) == None:

至:

代码语言:javascript
复制
if sheet.find(self.host.value) is None:

当我测试您的脚本时,

  • 可以与您复制相同的问题。并且,当此修改反映在脚本中时,我确认错误已被删除。

参考资料:

Python None comparison: should I use "is" or ==?相关的

  • 线程

在这种情况下,这一点很重要,因为如果存在一个针对x == y类的__eq__方法,那么__eq__就会将x转换为x,而gscedCell类实现了一个自定义__eq__方法,该方法尝试对other对象(other.row)进行无保护的属性访问,如果other不是一个Cell (在本例中,它抛出一个错误,但传入另一个具有row属性的类可能会有不同的行为)。

虽然这是一个非常糟糕的实践,但如果您使用is作为与None进行比较的规则,则可以避免这种做法。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73911302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档