Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;UnboundLocalError:局部变量';x';作业前参考“;但是它';这是一个全局变量_Python_Variables_Global_Local_Discord.py - Fatal编程技术网

Python &引用;UnboundLocalError:局部变量';x';作业前参考“;但是它';这是一个全局变量

Python &引用;UnboundLocalError:局部变量';x';作业前参考“;但是它';这是一个全局变量,python,variables,global,local,discord.py,Python,Variables,Global,Local,Discord.py,我正在使用Discord.py制作Python Discord机器人。我对Python比较陌生,但在其他语言方面有大约八年的经验。我的问题是,即使我已将前缀定义为全局变量,我还是会在赋值之前引用UnboundLocalError:local变量“prefix”。(我正在使用Python 3.6.8) 我曾尝试在_ready函数上定义之外的变量,但得到了相同的结果 import discord; client = discord.Client(); @client.event async de

我正在使用Discord.py制作Python Discord机器人。我对Python比较陌生,但在其他语言方面有大约八年的经验。我的问题是,即使我已将
前缀
定义为全局变量,我还是会在赋值之前引用
UnboundLocalError:local变量“prefix”。(我正在使用Python 3.6.8)

我曾尝试在_ready
函数上定义
之外的变量,但得到了相同的结果

import discord;

client = discord.Client();

@client.event
async def on_ready():
  print('Loading complete!')
  ...
  global prefix
  prefix = 'hello world'

@client.event
async def on_message(message):

  if message.author == client.user:
    return

  messageContents = message.content.lower()
  splitMessage = messageContents.split()
  try:
    splitMessage[0] = splitMessage[0].upper()lower()
  except:
    return
  if splitMessage[0]==prefix:
    ...
    (later in that if statement and a few nested ones)
    prefix = newPref

client.run('token')
我期望输出将是它在if语句中运行代码,但相反,我得到了错误
UnboundLocalError:assignment之前引用的局部变量“prefix”
,并且没有运行代码

我能想到的唯一问题是
prefix=newpref
。我试过:

global prefix
prefix = newpref
但是我得到了一个错误:
SyntaxError:name'prefix'在全局声明之前使用
,bot根本没有启动。我该怎么办

根据

这是因为当您对作用域中的变量进行赋值时, 该变量成为该范围的局部变量,并以类似方式隐藏 外部作用域中的命名变量

短版:

您需要在函数范围之外声明全局变量,例如

#declaring the global variable
x = 10
def foobar():
    #accessing the outer scope variable by declaring it global
    global x
    #modifying the global variable
    x+=1
    print(x)
#calling the function
foobar()
长版本:

基本上,python中的一切都是一个对象,这些对象的集合被调用

每个模块都有自己的专用符号表,用作 由模块中定义的所有函数生成的全局符号表。因此 模块作者可以在模块中使用全局变量,而无需 担心与用户的全局变量发生意外冲突

因此,当您运行bot脚本时,python将其视为作为主脚本运行的模块,具有自己的全局作用域。
在全局范围内声明变量时,要在某些函数的局部范围内使用它,需要使用关键字
global
来访问模块的全局范围

此外,python不需要分号来终止语句(例如
client=discord.client();
)。如果要将多个语句放在同一行上,可以使用分号分隔语句。

根据

这是因为当您对作用域中的变量进行赋值时, 该变量成为该范围的局部变量,并以类似方式隐藏 外部作用域中的命名变量

短版:

您需要在函数范围之外声明全局变量,例如

#declaring the global variable
x = 10
def foobar():
    #accessing the outer scope variable by declaring it global
    global x
    #modifying the global variable
    x+=1
    print(x)
#calling the function
foobar()
长版本:

基本上,python中的一切都是一个对象,这些对象的集合被调用

每个模块都有自己的专用符号表,用作 由模块中定义的所有函数生成的全局符号表。因此 模块作者可以在模块中使用全局变量,而无需 担心与用户的全局变量发生意外冲突

因此,当您运行bot脚本时,python将其视为作为主脚本运行的模块,具有自己的全局作用域。
在全局范围内声明变量时,要在某些函数的局部范围内使用它,需要使用关键字
global
来访问模块的全局范围


此外,python不需要分号来终止语句(例如
client=discord.client();
)。如果要将多条语句放在同一行上,可以使用分号来分隔语句。

是否按照错误消息的建议在函数中使用前缀之前尝试过全局语句?是否按照错误消息的建议在函数中使用前缀之前尝试过全局语句?