0

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?

12
  • We won't be able to help you with such a vague question. Provide the code you've written so far, your expected input and output, and a description of why it doesn't work. Commented Jul 19, 2016 at 11:14
  • @BenjaminHodgson I cant provide the entire code because it too much. The basic structure is there and I'm asking a question regarding updating variables across modules not about my code. Commented Jul 19, 2016 at 12:36
  • Also the code requires knowledge of matplotlib, so a regular programmer wouldn't understand it. So Im asking a general question about modules. Commented Jul 19, 2016 at 12:37
  • 2
    You don't want to pass y as an argument from the live_plotting(self,BOOLEAN, y ) function ? Commented Jul 19, 2016 at 13:17
  • 1
    If I understand correcly : you want that each time y is modified, self.y is also modified? maybe instead of using y you just use InstanceName.y where instanceName is the name of the object you instanciated. Mutator have to be defined in the class : def set_y(self,y) : self.y=y and you use it : InstanceName.set_y(value) Commented Jul 19, 2016 at 13:31

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.