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?
2 Answers
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
From what i've understand, mutator arn't what you are looking for. A mutator is a function within a class that access an attribut and modify it's value.
I think in your code, you want to use y as if self.y and y where linked to each others. But when you give y as an argument to a function, then self.y = y the y in the function is a copie of the y you passed as an argument. then self.y = y take the value of the copied y and store it in self.y.
After instanciating your object :
my_instance = class(x,y)
#if you want to modify my_instance.y
my_instance.y = value
or with a mutator in the class :
def set_y(self,value):
self.y=value
then in your main when you want to modify y :
my_instance.set_y(value)
yas an argument from thelive_plotting(self,BOOLEAN, y )function ?yis modified,self.yis also modified? maybe instead of usingyyou just useInstanceName.ywhere instanceName is the name of the object you instanciated. Mutator have to be defined in the class :def set_y(self,y) : self.y=yand you use it : InstanceName.set_y(value)