Python: Show content of hidden parameters for debugging
Question:
my core question is: how can I visualize hidden parameters of a class for debugging?
I’m using spyder 3.6 for coding and have e.g. this test code:
class ExampleClass():
def __init__(self, number):
self.__hiddenVar = number
self.visibleVar = number
print(str(self.__hiddenVar ) + 'at init')
def plus_2_times_4(x):
return(4*(x + 2))
def arithmetic(self):
print(str(self.__hiddenVar ) + 'at arithmetic')
return(ExampleClass.plus_2_times_4(self.__hiddenVar ))
instance = ExampleClass(number = 4)
instance.arithmetic()
Now I want to stop the run let’s say at the printout of "arithmetic" and debug the content of "self".
When I run "self.visibleVar" in console window of spyder, it shows the conent, in this case "4".
When I do the same with "self.__hiddenVar" it throws an error: *** AttributeError: ‘ExampleClass’ object has no attribute ‘__hiddenVar’
Well, I understand why this is the case: it is a hidden variable and this is how it is supposed to be. But how can I debug in that situation? Do I have any possibility to show the content even though it is a hidden variable?
Answers:
As the first thing, I would suggest you to read:
- What is the meaning of single and double underscore before an object name?
- How to access "__" (double underscore) variables in methods added to a class
To resume the content that you really need, I must say that the interpreter "transforms" __hiddenvar
to _ExampleClass__hiddenvar
.
my core question is: how can I visualize hidden parameters of a class for debugging?
I’m using spyder 3.6 for coding and have e.g. this test code:
class ExampleClass():
def __init__(self, number):
self.__hiddenVar = number
self.visibleVar = number
print(str(self.__hiddenVar ) + 'at init')
def plus_2_times_4(x):
return(4*(x + 2))
def arithmetic(self):
print(str(self.__hiddenVar ) + 'at arithmetic')
return(ExampleClass.plus_2_times_4(self.__hiddenVar ))
instance = ExampleClass(number = 4)
instance.arithmetic()
Now I want to stop the run let’s say at the printout of "arithmetic" and debug the content of "self".
When I run "self.visibleVar" in console window of spyder, it shows the conent, in this case "4".
When I do the same with "self.__hiddenVar" it throws an error: *** AttributeError: ‘ExampleClass’ object has no attribute ‘__hiddenVar’
Well, I understand why this is the case: it is a hidden variable and this is how it is supposed to be. But how can I debug in that situation? Do I have any possibility to show the content even though it is a hidden variable?
As the first thing, I would suggest you to read:
- What is the meaning of single and double underscore before an object name?
- How to access "__" (double underscore) variables in methods added to a class
To resume the content that you really need, I must say that the interpreter "transforms" __hiddenvar
to _ExampleClass__hiddenvar
.