So I know the way to make a variable "private" in python like this:
class Foo:
def __init__(self):
self.__private = 'bar'
This "works" and doesn't, as shown below:
foo = Foo()
'__private' in vars(foo) #False
'_Foo__private' in vars(foo) #True
Now, I understand this is the way to make private variables in python and I like this way. It allows you to mangle names so that no subclasses accidentally override this (because it begins with the class's name), and that nobody will accidentally use it. It also gives you the power to change the private variables if you know what you are doing. Also, it is the best way to do it, because truly private variables are impossible.
Or so I thought.
Recently, I was reading PEP 8 and I saw this line:
We don't use the term "private" here, since no attribute is really private in Python (without a generally unnecessary amount of work).
This quote is found in the Designing for Inheritance section of PEP 8.
Note the phrase "without a generally unnecessary amount of work". I am now sure that there must be a way to get truly private variables in python. How would I do that?
I have tried overriding __getattribute__
, but the problem is that there is no way to tell if the call is coming from inside the class or not (that I am aware of).
Also, the __dict__
attribute is annoying when trying to do this because it holds references to all instance variables.
I also thought of metaclasses, but those seem to have the same problems as __getattribute__
.
Thoughts?
Note: I understand that any way to make truly private variables in python should never be done in productive code. I just want to know how it could be done.
In Python, there is no existence of “Private” instance variables that cannot be accessed except inside an object.
In actual terms (practically), python doesn't have anything called private member variable in Python. However, adding two underlines(__) at the beginning makes a variable or a method private is the convention used by most python code.
Python doesn't have the concept called private variables. But, most of the Python developers follow a naming convention to tell that a variable is not public and it's private. We have to start a variable name with a double underscore to represent it as a private variable (not really). Example:- one, two, etc..,.
Private methods are those methods that should neither be accessed outside the class nor by any base class. In Python, there is no existence of Private methods that cannot be accessed except inside a class. However, to define a private method prefix the member name with the double underscore “__”.
You can get nearly the same effect without the fancy inspection by using closures instead of attributes.
class Foo:
def __init__(self):
private = 'bar'
def print_private():
print(private)
self.print_private = print_private
foo = Foo()
foo.print_private() # works
foo.private # kaboom
Of course, inspect
can see into closures too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With