mariano mariano - 4 months ago 8
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

y
and therefore I have an equivalent
self.y = y
in the
__init__
method. Now the problem is that in the main script I have a
for
loop that changes the
y
value and calls the method 'live_plotting' from the same class. I usually have to pass in the
y
value as an argument again and have another
self.y = y
inside the method. This is not neat, is there any way of updating
self.y
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

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.

Comments