4 ms·
Python absolutely does have private members. If a member starts with two underscores, its class name (and an underscore) is prefixed to the member name, essenti
by n2d4 1y ago
Python absolutely does have private members. If a member starts with two underscores, its class name (and an underscore) is prefixed to the member name, essentially resulting in the same behavior as a private modifier. (Although there are ways to circumvent this in Python, there are also ways to access private fields in most other languages.)
But even if that were true, that wouldn't mean that accessibility modifiers are unnecessary in other languages. Python is a certain language with certain idioms, and other languages are more or less OOP and hence some constructs that might not make sense in Python definitely make sense elsewhere.
- NeutralForest 1y agoI see what you mean what I wouldn't really qualify name mangling as private members, you're discouraged to access them but you can access them all the same.
- jjice 1y agoI'd consider it the same, personally. It's a social contract that you can absolutely break if you want, just with a bit of work. Less work in Python than it is Java, but there are ways to get to them in Java, or hell, just modify the source and change it directly. By doing that, you're doing something the developer didn't intend and you shouldn't be surprised if it doesn't work at some point in the future. Same as I'd expect in the Python case.
- NeutralForest 1y agoSure, I think the bar to modify is higher in Java for example but I can't really find fault in what you're saying because I'd never unknowingly modify a member with double underscores in Python.
- throwaway2037 1y ago> but you can access them all the same Can't you do the same in C++ using struct offsets and reinterpret_cast<>? I'm not sure what you are saying here.
- NeutralForest 1y agoI'm not familiar with C++ enough to know about circumventing `private` but in Python you can access "private" members just like any other "public" ones, with the dot notation. There's no specific enforcement at the language level, no error will be thrown.
- colejohnson66 1y agoSomething something undefined behavior
- TylerE 1y agoThat is absolutely a social construct and not a language feature. You can override “private” members just fine with a simple assignment.
- _ZeD_ 1y agoNo, it doesn't: double underscore field names are mangled basically to avoid easy overriding in su classes; keep in mind that it is a feature that predates super() in almost any form
- masklinn 1y agoNot only are those not technically private, they were never intended for information hiding in the first place. Name mangling was introduced to avoid namespace conflicts in inheritance scenarios.