为什么全球化布尔值不起作用而全球化字典能起作用 [英] Why does globalising a boolean not work but globalising a dictionary does
问题描述
这只是一个问题,想知道为什么这行不通.我想出了一个更好的方法,但是我不知道为什么以前它不起作用.
This is just a question wondering why this doesn't work. I have figured out a better way, but I don't know why previously it wasn't working.
global mydict
mydict = {}
这似乎很好用,并使mydict词典成为全局词典.我什至打印出mydict,它的工作原理.但是,这样做:
This seems to work fine, and has made the mydict dictionary global. I even print mydict and it works. However, doing this:
global bool
bool = False
似乎无效.如果尝试在我的代码中打印布尔值,我会得到:
Does not seem to work. If trying to print bool in my code, I get:
UnboundLocalError: local variable 'bool' referenced before assignment
那为什么它对字典而不是布尔型有效?
So why does it work for the dictionary and not the boolean?
哦,如果有人想知道如何找到更好的方法,我会初始化一个类,并通过以下操作使bool在该类中成为全局变量:self.bool = False起作用了.我是从以下问题中得到的:将所有变量设置为全局
Oh, also, if anyone was wondering how I figured out a better way, I initialised a class and made bool global in the class by doing: self.bool = False which worked. I got it from this question: Making all variables global
根据要求,我将发布必要的代码:
As requested, I'll post the necessary code:
import chatbot
global mydict
mydict = {}
global haveamessage
haveamessage = False
class MyBot(chatbot.ChatBot):
def __init__(self, username, password, site):
chatbot.ChatBot.__init__(self,username,password,site)
def on_message(self, c, e):
print mydict
print haveamessage
if __name__ == '__main__':
bot = MyBot("MyUsername", "MyPassword", "MySite")
bot.start()
我将尝试解释此代码. chatbot模块几乎允许用户在Wikia上的Wiki中创建bot,该公司允许创建任何人都可以编辑的wiki. Wiki上有一个聊天扩展,用户可以与之聊天.该脚本允许机器人加入聊天并执行命令.当有人在聊天中发布内容时,on_message()熄灭.
I'll try explain this code. Pretty much the chatbot module is to allow users to create bots in wikis on Wikia, a company that allows wikis to be created which anyone can edit. On a wiki there is a chat extension where users can talk to. This script allows a bot to join the chat and do commands. on_message() goes off when someone posts something in the Chat.
因此打印:
{}
Traceback (most recent call last):
File "file.py", line 146, in <module>
bot.start()
File "/Library/Python/2.7/site-packages/chatbot.py", line 371, in start
self.on_message(self.c, e)
File "file.py", line 12, in on_message
print haveamessage
UnboundLocalError: local variable 'haveamessage' referenced before assignment
我想澄清的是,这不会对所有人产生错误,是因为您不在Wikia聊天中.函数on_message()仅在有人在聊天室中发布内容时运行.例如,我可能有:
I'd like to clarify that the reason this isn't producing an error for all of you is because you are not in a Wikia chat. the function on_message() only runs when someone posts something in the Chat. For example, I may have:
def on_message(self, c, e):
if e.text == 'hello': # e.text is what a user posts in the chat. e = event
c.send('Hello!') # c.send simply sends back a message in the chat. c = connection
因此,当有人在聊天中打招呼时,Bot会回发Hello!
So when someone posts in chat hello, the Bot posts back Hello!
推荐答案
您发布的代码不会产生您声称的错误.但是,在函数外部使用global
关键字无效,因此,它不能按预期工作也就不足为奇了.
The code you've posted does not produce the error you claim it does. However using the global
keyword outside of a function has no effect, so it's not surprising that it doesn't work like you expect.
我假设在您的真实代码中,您实际上是在尝试分配给on_message
内部的haveamessage
.如果是这样,则在该方法内需要一个global
语句.
I assume that in your real code, you're actually trying to assign to haveamessage
inside on_message
. If so, you need a global
statement inside that method.
基本规则是:如果尝试从函数中分配全局变量,则需要在该函数中使用global
关键字.否则,您根本不需要global
关键字.变量是否为布尔值没有区别.
Basically the rule is: If you try to assign a global variable from within a function, you need to use the global
keyword within that function. Otherwise you don't need the global
keyword at all. Whether or not the variable is a boolean makes no difference.
这篇关于为什么全球化布尔值不起作用而全球化字典能起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!