What if I keep my class members public?
In C ++ instance variables are private by default, in Python variables are public by default
I have two questions regarding the same: -
1: why does Python have all members public by default?
2: People say your data should be private, what if I make my data public? What are the disadvantages of this approach? why is this bad design?
a source to share
You can use leading underscores in a title to tell readers of the code that the specified name is an internal detail and should not rely on it for future releases. An agreement like this is really all you need - why weigh the language with an enforcement mechanism?
Data, like methods, must be publicly available (no main underscore) if it is part of your API, developed by a class that you intend to support in the future. In C ++ or Java, this is unlikely to happen, because if you want to change a data member to an accessor, you're out of luck - you will have to break your API and every client of that class will have to change.
In Python and other languages that support the property
-like construct , this is not the case - you can always replace a data item with a property that transparently calls accessor methods, the API does not change, and the client code does not change.Thus, in Python and other languages with type constructs property
(I believe NET languages like at the source level, but not necessarily at the bytecode level), you can also leave your data public when it's part of the API and no accessors are needed (you can always add accessors to later implementation versions if needed, rather than breaking the API).
So this is not a general problem with OO, but a specific language: does the given language support a property-like construct. Python does.
a source to share
I can't comment on Python, but in C ++, frameworks provide public access by default.
The main reason you need a private part of your class is because without it, you cannot guarantee that your invariants are met. For example, if you have a string class that needs to keep track of the length of a string, you need to be able to keep track of inserts. But if the underlying member char*
is public, you cannot do that. Anyone can just come in and put something on the end, or overwrite the null terminator, or call it delete[]
, or whatever. When you call your member length()
, you just have to hope for the best.
a source to share
This is really a matter of the philosophy of language. I prefer the Python camp, so I can go down a bit hard in the C ++ style, but in the end in C ++ you can forcefully deny users of your class access to some internal parts.
In Python, it is a matter of convention and the claim that it is internal. Some applications may require access to an internal member for malicious purposes (such as documentation generators). Some users who know what they are doing may want to do the same. People who want to shoot in the leg, woven by internal details, are not immune to suicide.
As Dennis said, "Anyone can just come in and put something on the end or overwrite the null terminator." Python treats the user like an adult and expects her to take care of herself. C ++ protects the user as well as the child.
a source to share