0

I have a pubnub callback class (callback), with a message handler function (message) inside it. The handler function is called by pubnub when it receives a message, and it should take a variable which is defined outside of the class (in another class and function) and modify it based on the message that it has received from pubnub. This updated variable should remain globally available. I've tried putting global keywords all over the place, but it hasn't helped:

class callback(SubscribeCallback):  # Listener for channel list

    def message(self, pubnub, message):
        new_var = set(var) - set(message)
        # do stuff with new_var
        var = new_var #Update global variable (it doesn't, but it should!)


class other(django admin command thing):
    def command(self):
        var = ['a', 'b'] #Initial definition - this function is only called once

Where does the global keyword need to go to make this work? I can access and pop from var2, but I can't both read and write var...

7
  • In command() before var is defined. Commented Mar 9, 2018 at 21:50
  • I don't see a global variable (outside of the classes) and I don't see the global keyword in the functions that are supposed to modify a global variable. The global keyword is required for alle function where you modify the global variabl, unless the variable is mutable and you modify it with methods inplace. Commented Mar 9, 2018 at 21:53
  • @kindall That doesn't work - the error is: UnboundLocalError: local variable 'var' referenced before assignment, in the new_var = set(var) - set(message) line Commented Mar 9, 2018 at 21:54
  • @MikeScotty No, I haven't included it in this sample code, because I don't know where to put it. I've tried putting it in various places, none worked. I thought it would be clearer without my guesses! Commented Mar 9, 2018 at 21:55
  • @kindall It is defined, and that function is called, first global var, and then var = something Commented Mar 9, 2018 at 21:58

1 Answer 1

1

You need to declare the variable as global in each context where it's referenced

>>> class A:
...     def test(self, m):
...             global X
...             X = X + m
...
>>> class B:
...     def __init__(self):
...             global X
...             X = "Y"
...
>>> b = B()
>>> X
'Y'
>>> a = A()
>>> a.test("Z")
>>> X
'YZ'
Sign up to request clarification or add additional context in comments.

3 Comments

This worked, oddly, the vim ALE linting thing says that it shouldn't... /shrug Thanks!
@Alex read this thread, not only the accepted answer but also (at least) the next answer to that one: stackoverflow.com/questions/423379/…
@progmatico Thank you, that makes a lot more sense. I did come across that thread, but I still wasn't clear on where the global keywords should go. Thanks for the link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.