Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does globalising a boolean not work but globalising a dictionary does
    text
    copied!<p>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.</p> <pre><code>global mydict mydict = {} </code></pre> <p>This seems to work fine, and has made the mydict dictionary global. I even print mydict and it works. However, doing this:</p> <pre><code>global bool bool = False </code></pre> <p>Does not seem to work. If trying to print bool in my code, I get:</p> <pre><code>UnboundLocalError: local variable 'bool' referenced before assignment </code></pre> <p>So why does it work for the dictionary and not the boolean?</p> <p>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: <a href="https://stackoverflow.com/questions/10775411/making-all-variables-global">Making all variables global</a></p> <hr> <p>EDIT: As requested, I'll post the necessary code:</p> <pre><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() </code></pre> <p>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.</p> <p>So this prints:</p> <pre><code>{} Traceback (most recent call last): File "file.py", line 146, in &lt;module&gt; 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 </code></pre> <hr> <p>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:</p> <pre><code>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 </code></pre> <p>So when someone posts in chat hello, the Bot posts back Hello!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload