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...

Alex
  • 2,270
  • 3
  • 33
  • 65
  • In `command()` before `var` is defined. – kindall Mar 09 '18 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. – Mike Scotty Mar 09 '18 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 – Alex Mar 09 '18 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! – Alex Mar 09 '18 at 21:55
  • @kindall It is defined, and that function is called, first `global var`, and then `var = something` – Alex Mar 09 '18 at 21:58
  • can you print the value of global variable inside the class before assigning new value to it in the class? – user1779646 Mar 09 '18 at 22:03
  • @user1779646 I tried to, and no, same error as above: `UnboundLocalError: local variable 'var' referenced before assignment` – Alex Mar 09 '18 at 22:05

1 Answers1

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'
rdowell
  • 729
  • 4
  • 15
  • This worked, oddly, the vim ALE linting thing says that it shouldn't... /shrug Thanks! – Alex Mar 09 '18 at 22:41
  • 1
    @Alex read this thread, not only the accepted answer but also (at least) the next answer to that one: https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them – progmatico Mar 09 '18 at 23:04
  • @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 – Alex Mar 10 '18 at 02:38