- attached test.py
Display variables inside of a class
If a class with some variables is created via python script, these variables are not always displayed in the variables widget. See attached file as example.
Comments (3)
-
-
- changed status to resolved
-
Hi David,
Johann was right to set this issue to 'resolved', however the explanation is slightly different:
In your example:
# coding=iso-8859-15 import numpy as np class test(): global workingDir workingDir = "C:" startDir = "" upDating = True measureType = 0 plotElementType = 0 result = {} def __init__(self): pass def makeDict(self): result["bla"] = 1 err = test()
all variables are no member variables of the instance 'err' of the class 'test', but they are static variables of the class 'test' itself. If you would create another instance 'err2 = test()', 'err' and 'err2' would share the same variables workingDir, startDir... Usually, these variables should be considered to be constants. They are bound to the class itself and not to each instance, therefore they are not displayed as child of the instance in the workspace widget. If you want to create member variables, you have to create them in the constructor and prepend a variable 'self.':
# coding=iso-8859-15 import numpy as np class test(): def __init__(self): self.workingDir = "C:" self.startDir = "" self.upDating = True self.measureType = 0 self.plotElementType = 0 self.result = {} def makeDict(self): result["bla"] = 1 err = test()
Then, access them by 'self.xxx' in any other member method of the class.
- Log in to comment
The variables within the class are "local variables". You can use a breakpoint within the class to see the variables in the "local variables" list.
You can also explicitly define variables as global as it is done in the attached test.py Variables can also be returned by a function to get it outside this function.