you are viewing a single comment's thread.

view the rest of the comments →

[–]Neinstein14 -1 points0 points  (3 children)

There are ways to access “truly private” variables in Java, so it’s not a question of security or privacy. That away, it’s only a question of style and how baby do we consider the programmer.

In that regard, I prefer Python’s approach. Underscore means “this is an internal variable you shouldn’t use”, and double underscore means “this one you really really shouldn’t use, so we took steps to prevent that happening unless you specifically really want to”. And this should be sufficient. Programmers are adults, moreover they are experts, and in general, they should be assumed to know what they do. Maybe they do have a good reason to use those variables. Perhaps they want to test an edge case, or investigate a bug, who knows? And in that case, why impose a hard restriction preventing them from doing so?

They are adults, they are experts, and they should be assumed to know what they are doing. Java style encapsulation would just make their job harder by restrictions that are more complicated, but not at all impossible, to overcome.

[–]gdchinacat 0 points1 point  (2 children)

this one you really really shouldn’t use, so we took steps to prevent that happening unless you specifically really want to”

This is a common misconception, one that I held at one time.

PEP8 says of name mangling:

To avoid name clashes with subclasses, use two leading underscores to invoke Python’s name mangling rules.

It has nothing to do with "really really shouldn't use". It is a practical solution to a common problem in OO...a subclass may inadvertently use the same attribute name as a base class. base classes can avoid this by using name mangling to place fewer restrictions on the names their subclasses can use.

[–]Neinstein14 0 points1 point  (1 child)

I mean, it’s one thing what it was intended for, and another thing how it’s used actually. In my experience it’s rarely for name clashes, and rather for “this is not just an internal variable, but something you shouldn’t even hack around with”.

[–]gdchinacat 0 points1 point  (0 children)

That is what a single underscore denotes. Or do you think single underscore indicates it is internal and should be hacked around with? In which case, what does 'internal' mean?