[英]How to change the value of a variable in a function from a different file in Python?
[英]How to change value of variable from different python file referenced different class?
我在不同的 python 文件中有一個主 python 文件和一個 class 文件。 我試圖從 class 更改 python 文件中的變量。 但是,我不知道如何在__init__
function 之外執行此操作。
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
主文件:
from Matrices import Matrices
otherControls = {"right": True, "left": True}
Matrices(memory, matricesFrame, tempBoolsControl, otherControls).createNewMatrix()
這意味着更改主文件中的otherControls
變量,但它僅在本地更改它。 我無法訪問__init__
function 之外的原始otherControls
變量。 任何人都可以幫忙嗎?
如果將矩陣 object 保存到變量中,例如
mat = Matrices(memory, matricesFrame, tempBoolsControl, otherControls).createNewMatrix()
您可以像這樣訪問otherControls
屬性:
mat.otherControls = {'right':True, 'left':False}
# or if you want only one of the keys
mat.otherControls['right'] = False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.