I know that the attributes of class which are declared by double underscore __ prefix may or may not visible outside the class definition. As we can still access those attributes by object._className__attrName.
class A:
def __init__(self):
self.a = 1
self.b = 2
----
----
self.z = 26
self.catch = 100
Now to protect all attributes except the attribute catch, I have to declare them with double underscore which is quite messy. Is there a way I can say in my class definition that only self.catch can be accessed outside the class?
Apologies if this is answered somewhere else or discussed earlier.
Yes, it is possible to hide private data in a closure — at least, if there is a way to access
privatefrom outsidemake_A, I haven’t found it:Notice that
privateis not indir(a)Although this is possible, I do not think this is recommendable way to program in Python.
Above,
privateis shared by all instances ofA. To use private data on a per-instance basis, addselfto the dict key: