Hidden variables
Last updated: Oct 09, 2024
You can hide data by creating Private variables. Private variables can be accessed
only by the class itself. If you declare names of the form
or
__xxx
, that is with two preceding underscores, the Python parser will
automatically add the class name to the declared name, creating hidden variables. For example:__xxx_yyy
class MyClass: __attr = 10 #private class attribute def method1(self): pass def method2(self, p1, p2): pass def __privateMethod(self, text): self.__text = text #private attribute
Unlike in Java, in Python all references to instance variables must be qualified with
; there's no implied use of self
.this
Was the topic helpful?
0/1000