简体   繁体   English

Class 变量是 NoneType 即使定义了属性

[英]Class variable is NoneType even though attribute was defined

I have a class that looks like this:我有一个看起来像这样的 class:

class DatabaseTest:

    def __init__(self):

        self.headers = {
            "host": os.environ["HOST"],
            "username": os.environ["USERNAME"],
            "password": os.environ["PASSWORD"]
        }

        self.db = None
        self.cursor = None
        asyncio.create_task(self._connect())

    
    async def _connect(self):
        
        self.db = await aiomysql.connect(self.headers['host'], self.headers['username'], self.headers['password'])
        self.cursor = await self.db.cursor()
        await self.cursor.execute('use bot')


    
    async def _insertData(self, guildid):

        sql = f'insert into data(guild) values ({guildid})'

        await self.cursor.execute(sql)

Whenever I do await DatabaseTest()._insertData(123456789) it gives an error saying that self.cursor is NoneType when I clearly redefined what self.cursor is in _connect.每当我await DatabaseTest()._insertData(123456789)时,当我清楚地重新定义 self.cursor 在 _connect 中时,它会给出一个错误,提示 self.cursor 是 NoneType。 I know that _connect was called because I put a print statement right at the end and it printed.我知道调用了 _connect 是因为我在最后放置了一个打印语句并打印了它。 How would I fix this?我将如何解决这个问题?

The traceback is this (im using it for a discord bot):回溯是这样的(我将它用于 discord 机器人):

Traceback (most recent call last):
  File "C:\Users\Name\PycharmProjects\compscibot\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Name\PycharmProjects\compscibot\cogs\startup\Help.py", line 57, in cmdchannel
    channelid = await DatabaseTest().getData(ctx.guild.id,'cmdchannel')
  File "C:\Users\Name\PycharmProjects\compscibot\cogs\dbtest.py", line 75, in getData
    await self.cursor.execute(sql)
AttributeError: 'NoneType' object has no attribute 'execute'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Name\PycharmProjects\compscibot\venv\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Name\PycharmProjects\compscibot\venv\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Name\PycharmProjects\compscibot\venv\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'execute'
None

The context is not switched until await .直到await才会切换上下文。

asyncio.create_task() just schedules an execution. asyncio.create_task()只是安排执行。

Using asyncio.sleep() :使用asyncio.sleep()

db = DatabaseTest()
await asyncio.sleep(0)
db._insertData(123456789)

or refactor:或重构:

class DatabaseTest:
   def __init__(self):
        self.headers = {
            "host": os.environ["HOST"],
            "username": os.environ["USERNAME"],
            "password": os.environ["PASSWORD"]
        }

    async def __aenter__(self):
        await self._connect()
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        # cleanup
        await self.cursor.close()
        self.db.close()

    ...
async with DatabaseTest() as db:
    await db._insertData(123456789)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM