mariano mariano - 1 year ago 76
Python Question

How do I update a variable value across modules Python?

I have a main script and I am importing another module with a class in it. When I instantiate the class one of the arguments is

and therefore I have an equivalent
self.y = y
in the
method. Now the problem is that in the main script I have a
loop that changes the
value and calls the method 'live_plotting' from the same class. I usually have to pass in the
value as an argument again and have another
self.y = y
inside the method. This is not neat, is there any way of updating
across modules without having to pass in as an argument again? I thought of using pointers but apparently everything is a pointer in python. Can anyone offer an alternative solution?

Answer Source

I'm not really sure if it's what you want, but from my understanding you want to update the value of self.y without a method?

This would be done by executing

object_name.y = new_value

where object_name is the name you gave the instance of your Class, seeing as self variables declared in the __init__ are attributes that belong to that instance of the class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download