Prerequisite: Output: In the above program, we tried access a hidden variable outside the class using an object and it threw an exception. We can access the value of a hidden attribute using a tricky syntax:
# Hidden member MyClass
# Member method that changes
# Driver code
MyClass () p>
) p >
# This line raises an error
(myObject .__ hiddenVariable)
2 7 Traceback (most recent call last): File "filename.py", line 13, in print (myObject .__ hiddenVariable) AttributeError: MyClass instance has no attribute '__hiddenVariable'
In the above program, we tried access a hidden variable outside the class using an object and it threw an exception.
We can access the value of a hidden attribute using a tricky syntax:
Private methods are available outside of their class, but not as easily accessible. Nothing in Python is truly private; internally, the private method and attribute names are mapped and unmangled on the fly to make them appear inaccessible by their given names [see this for the source].
From str method of Test: a is 1234, b is 5678 [Test a: 1234 b: 5678]
Important points about printing:
Output: p >
Test a: 1234 b: 5678
This article courtesy of Shwetanshu Rohatgi . Please post comments if you find something wrong or if you would like to share more information on the topic discussed above.