0

Here is an example:

class ExpensesCalculator:
  __max_number_of_people = 3

  def __init__(self):
    pass

if __name__ == "__main__":
  calculator = ExpensesCalculator()
  calculator._ExpensesCalculator__max_number_of_people = 24
  print(calculator._ExpensesCalculator__max_number_of_people)

As we can see, the private variable "__max_number_of_people" is accessible outside the class with "calculator._ExpensesCalculator__max_number_of_people".

So, what's the use of making that variable private if it can still be modified outside the class in the way described above?

Why are private variables "masked" instead of being made "private" in Python?

  • 4
    Because there is no such thing as actually being private in python, what you see is what python considers private. – luk2302 Jun 29 '22 at 12:08
  • 1
    It's not to guarantee that the variable can never be touched. Private member variables in C++ can be messed with if you know the right incantation, just like with Python. It's to say "hey, you're putting in a lot of effort to mess with this, are you sure that's the right approach?". Just like a "keep off the grass" sign doesn't usually have an automated turret enforcing it, you're expected to observe the sign and keep off the grass. As per the Python docs: "Note that the mangling rules are designed mostly to avoid accidents". – Kemp Jun 29 '22 at 12:09
  • 2
    There is no such thing as a "private" class variable in Python. The double leading underscore is merely a convention – DarkKnight Jun 29 '22 at 12:09
  • 1
    Also note that "for internal use only" convention is denoted by single leading underscore. In this case you use double leading underscore which is used for name mangling. i.e. this is not even "for internal use only" variable. I suggest to check https://stackoverflow.com/q/1301346/4046632 – buran Jun 29 '22 at 12:11
  • So, okay! If the variables are not exactly "private" in Python, why was this design decision made? I am assuming other languages implement the concept of "private" variables like it is supposed to be (please correct me if I am wrong). So, what is the main reason why Python did not follow the conventions of other languages? – chess_madridista Jun 29 '22 at 12:13
  • 1
    Everything in Python is accessible. You can just make attribute access a bit more tedious with two leading underscores or strongly indicate "please don't touch" with one leading underscore, but in the end Python is a language for consenting adults. If you document some attributes as internal and somebody wants to touch them anyway, whatever happens is their fault. – timgeb Jun 29 '22 at 12:13
  • 1
    Does this answer your question? [Does Python have “private” variables in classes?](https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes) – luk2302 Jun 29 '22 at 12:14
  • 1
    @chess_madridista the idea is that you might have a good reason to access those attributes which the developer didn't think of. It's enough to indicate "danger zone" by convention, not by enforcing it. – timgeb Jun 29 '22 at 12:15
  • 2
    If you're trying to protect your object from malicious modification by other code *in the same application* then something may have gone wrong in your dev process ;) If the variable is private to indicate that it's a bit of state that other code shouldn't mess with then the name mangling is sufficient to avoid accidentally modifying it in the situation where you forgot that was the case. "Has no attribute named...? Oh right, I have to use the accessor." – Kemp Jun 29 '22 at 12:16
  • Thank you for all the answers! I really appreciate it :). But I am sorry that those answers did not answer my question completely. Let me rephrase my question. Why do other programming languages have real "private" variables whereas Python only has a naming convention to differentiate between private and normal variables? – chess_madridista Jun 29 '22 at 12:21
  • 1
    @chess_madridista the designers of those other languages are control freaks, obviously. :) – timgeb Jun 29 '22 at 12:32
  • 2
    Why do we have different flavours of ice cream? – buran Jun 29 '22 at 12:34
  • 1
    In Python you can access the private member if you know how to mangle the name and make the effort to do so, knowing that it's not what you're intended to do. In C++ you can access the private member via at least two means (just counting ones that I can think of off the top of my head) if you make the effort to do so, knowing that it's not what you're intended to do. In the end it's just about how hard the language designer wants to make it. In both cases you're knowingly doing something you're not supposed to. The two languages just have different lines you have to cross. – Kemp Jun 29 '22 at 13:35

0 Answers0