我对 Python 全局有问题
I've problems with Python global
我的问题类似于,但不是class。
我的代码是这样的(删除一些有影响的行之后):
@Bot.command()
async def Limonate(ctx, secondi, menzione=None):
global attesa
global canale
attesa = float(secondi)
canale = ctx.channel
这是一个命令,我需要那个部分来执行此操作:
@Bot.event
async def on_ready():
attesa = 100
while (True):
frase = choice(frasi_ad_effetto)
await sleep(attesa)
global canale # Here's the problem
global menzione
await canale.send(menzione + ', ' + frase) #
所以问题是,如果 Bot
刚刚开始(并且 command
Limonate
从未执行过),canale
变量为空。
你知道解决我的问题的方法吗?
在函数Limonate
中,需要先声明canale
为全局变量,然后再给它赋值
@Bot.command()
async def Limonate(ctx, secondi, menzione = None):
global attesa, canale
attesa = float(secondi)
canale = ctx.channel
我的问题类似于
我的代码是这样的(删除一些有影响的行之后):
@Bot.command()
async def Limonate(ctx, secondi, menzione=None):
global attesa
global canale
attesa = float(secondi)
canale = ctx.channel
这是一个命令,我需要那个部分来执行此操作:
@Bot.event
async def on_ready():
attesa = 100
while (True):
frase = choice(frasi_ad_effetto)
await sleep(attesa)
global canale # Here's the problem
global menzione
await canale.send(menzione + ', ' + frase) #
所以问题是,如果 Bot
刚刚开始(并且 command
Limonate
从未执行过),canale
变量为空。
你知道解决我的问题的方法吗?
在函数Limonate
中,需要先声明canale
为全局变量,然后再给它赋值
@Bot.command()
async def Limonate(ctx, secondi, menzione = None):
global attesa, canale
attesa = float(secondi)
canale = ctx.channel