0

I have a main python file and a class in a different python file. I have tried to change a variable in the python file from the class. However, I don't know how to do this outside of the __init__ function.

The class:

class Matrices:
    currentMenuItem = 0
    dataForMatrix = {}

    def __init__(self, memory, matricesFrame, tempBoolsControl, otherControls):
        self.memory = memory
        self.matricesFrame = matricesFrame
        self.tempBoolsControl = tempBoolsControl
        self.otherControls = otherControls

    def createNewMatrix(self):
        self.otherControls["right"] = False

The main file:

from Matrices import Matrices

otherControls = {"right": True, "left": True}

Matrices(memory, matricesFrame, tempBoolsControl, otherControls).createNewMatrix()

This is meant to change the otherControls variable in the main file, but it only changes it locally. I can't access the original otherControls variable outside of the __init__ function. Can anyone help?

1
  • local variable othercontrols != class attribute self.othercontrols Commented Oct 9, 2019 at 20:08

1 Answer 1

1

If you save the Matrices object to a variable, e.g.

mat = Matrices(memory, matricesFrame, tempBoolsControl, otherControls).createNewMatrix()

you can access the otherControls attribute like this:

mat.otherControls = {'right':True, 'left':False}
# or if you want only one of the keys
mat.otherControls['right'] = False
Sign up to request clarification or add additional context in comments.

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.