I want to create an instance of a class that holds an external variable. The class should keep updating the external variable that is being changed continuously.
Here is my code that will explain what I mean to say. However this code will not update the variable assigned to the class because my approach is not correct.
class Update_variable(QtCore.QThread):
def __init__(self, VariablName):
QtCore.QThread.__init__(self)
self.VariablName = VariablName
def run(self):
while True:
variable = self.VariablName
print("Value updated to: ", variable)
time.sleep(1.0)
if __name__ == '__main__':
var1 = 0.0
var2 = 1.1
update_var1 = Update_variable(var1)
update_var2 = Update_variable(var2)
update_var1.start()
update_var2.start()
while True:
var1 = random()
var2 = random()
print("New value for var1: ",var1)
print("New value for var2: ",var2)
time.sleep(1.0)
You can't do this way, because variables are passed by assignment.
But the Python documentation itself gives us some options: https://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.