-1

I have a written python code for real time system in which I am acquiring the data from one functions and assigning it to a variable which is actually a global variable but when I tried to use it in other functions I am unable to get updated value in other functions. How to get rid of this problem. Sample code which will give an elaborate idea what I am exactly doing is as follows.

obtained_value = 0 

### Inside the continuous loop statement ###
def Startloop(self):
    global obtained_value
    obtained_value = acquire_data()  ## obtained value is getting updated after every 0.25 seconds ##
    ## other statements ##

def some_functions(self):
    ### want to do something which depend on value ###
    print obtained_value             ## does not print value inside "obtained_value"
    val1 = obtained_value * 10
    print val1
    ## etc etc ### 

I am not getting any value printed corresponding to print statement inside the some_function. Its seems like value of global variable is not getting updated. How to fix this problem. Any suggestion will be appreciated. Thanks in advance.

Ankush
  • 53
  • 2
  • 10
  • 1
    Why are you using `global`? See http://stackoverflow.com/q/19158339/3001761 – jonrsharpe Apr 17 '15 at 14:43
  • what is a `acquire_data()` returning? print it out – Pynchia Apr 17 '15 at 14:49
  • 1
    uhm... I just noticed your functions really look like methods. If so, then where is `obtained_value` declared? within the class? If that is the case, you should refer to it with `self.obtained_value`. And there is no need to declare it `global`, of course. – Pynchia Apr 17 '15 at 14:54
  • Actually it contains some decimal value and based on that value I have to check and test some important conditions of my system. – Ankush Apr 17 '15 at 14:57
  • OK. Please answer my questions regarding you functions/methods and I might be able to help you. Are the functions above part of a class? If so, they are methods and there is no need for global. You then need to decide if `obtain_value` is going to be a class variable or an object (class instance) variable. – Pynchia Apr 17 '15 at 14:59
  • I realise I still cannot get rid of C parlance. In my prev comment, "where is obtained_value declared?" I should have written "where is obtained_value first assigned?". In python there's no declaration. It's where you assign variables that matters. Just one of those little things.. – Pynchia Apr 17 '15 at 15:06
  • All other functions are part of the same class except the function which is returning me the acquired value i.e acquire_data(). – Ankush Apr 17 '15 at 15:06
  • I have assigned it first time inside the Startloop function itself. – Ankush Apr 17 '15 at 15:10
  • OK. I'd go for the instance variable, second option in my answer below. It depends on how you are going to use the class/objects. – Pynchia Apr 17 '15 at 15:39
  • Also note: if `obtained_value` is not an integer, you should initialise it to a constant of the proper type (e.g. `obtained_value = 0.0`) – Pynchia Apr 17 '15 at 15:44

2 Answers2

1

Then it appears Startloop and some_functions are instance methods, let's say of class Xyz.

If you want obtained_value to be a class variable, which is shared among the objects (instances) of the class, then assign it first within the class:

class Xyz(object):
    obtained_value = 0.0

    def Startloop(self):
        self.obtained_value = acquire_data()  ## obtained value is getting...
        ## other statements ##

    def some_functions(self):
        ### want to do something which depend on value ###
        print self.obtained_value             ## does not print value...
        val1 = self.obtained_value * 10
        print val1

Otherwise, if you want obtained_value to be an object variable, which belongs to a specific object (instance) of the class, then assign it in its constructor:

class Xyz(object):

    def __init__(self):
        self.obtained_value = 0.0

    def Startloop(self):
        self.obtained_value = acquire_data()  ## obtained value is getting...
        ## other statements ##

    def some_functions(self):
        ### want to do something which depend on value ###
        print self.obtained_value             ## does not print value...
        val1 = self.obtained_value * 10
        print val1

The key point is that Python has the concept of namespaces (please read on, don't be scared!) In the first case, obtained_value is assigned to the class' namespace. In the second case, obtained_value is assigned to the object's namespace. obtained_value is simply a name that is added to the appropriate namespace.

As you can see, obtained_value is reached the same way in both cases, through self.obtained_value. That is because Python looks for obtained_value in the object's namespace. If it does not find it there, Python looks for it in the namespace of the object's class.

This answer is by no means exhaustive, but I hope it helps. In case, please see the official docs.

Pynchia
  • 10,996
  • 5
  • 34
  • 43
  • I have tried the way you have suggested in this reply. But none of them is giving me gives me any value. Even the "print self.obtained_value" statement inside the function "some_functions(self)" do not print any value on terminal. Is their any other way to get updated value in "some_functions(self)" function. – Ankush Apr 18 '15 at 08:03
  • please post some more code. How you use the class, etc. – Pynchia Apr 18 '15 at 10:28
-1

Read these two lines again:

global value
obtained_value = acquire_data()

Spoiler:

value != obtained_value
André Laszlo
  • 15,169
  • 3
  • 63
  • 81
  • Firstly thank you for replying and extremely sorry for mistake that I have committed while my post. I have edited my question. Please give your valuable suggestion for the corrected post. – Ankush Apr 17 '15 at 14:44