pythonclassglobal-variables

Python- Pass a global Variable (that is constantly being updated) into a Class


I am trying to instantiate a class using a constantly updating global variable as an argument. But what is happening is the class (which runs in its own thread) won't recognise the global variable changing, it treats it as the value it was when passed. I could directly reference the global variable within the class, but I wanted to know if there is a method to pass it when instantiating it to save me repeating codes - one for every different global variable?

With the below code, the global variable is updating, but not within the class:

` import threading import time

# Global variable
global_number = 0

class GlobalVarClass:
    def __init__(self, global_var):
        """
        Initializes the class with a global variable.
        """
        self.global_var = global_var

    def print_value(self):
        """
        Prints the current value of the global variable.
        """
        print(f"Current Global Variable Value: {self.global_var}")

def increment_global_variable():
    """
    Increments the global variable by 1 every second.
    """
    global global_number
    while True:
        time.sleep(1)  # Wait for 1 second
        global_number += 1  # Increment the global variable by 1
        print(f"Incremented Global Variable: {global_number}")

def main():
    # Create an instance of the class and pass the global variable as an argument
    global_instance = GlobalVarClass(global_number)

    # Start the thread to increment the global variable
    increment_thread = threading.Thread(target=increment_global_variable, daemon=True)
    increment_thread.start()

    # Continuously print the current value of the global variable
    while True:
        global_instance.print_value()
        time.sleep(2)  # Print the value every 2 seconds to see the updates

if __name__ == "__main__":
    main()`

Solution

  • The problem you're facing is that in Python integers are immutable. When you initialize self.global_var you're getting the value of that variable rather than a reference to the global global_var.

    You're going to have to reconsider your approach. This feels rather like an XY problem. Why do you want to do this?

    Now, if you're working with something mutable you still don't get a reference to the global variable, but you do get a reference to the same object the global variable is referencing.

    >>> class Int:
    ...     def __init__(self, v):
    ...         self.v = v
    ...     def incr(self):
    ...         self.v += 1
    ... 
    >>> class B:
    ...     def __init__(self, x):
    ...         self.x = x
    ...     def print(self):
    ...         print(self.x.v)
    ... 
    >>> b = B(a)
    >>> a.incr()
    >>> a
    <__main__.Int object at 0x10bc6c1a0>
    >>> a.v
    44
    >>> a = Int(42)
    >>> b = B(a)
    >>> b.print()
    42
    >>> a.incr()
    >>> b.print()
    43
    >>>
    

    You could but probably shouldn't access global variables by name at runtime using the globals function.