Private Access Modifier
This article has been translated automatically from Russian to English. The original is available in Russian.
Private Access Modifier
Some languages have so-called access modifiers. They change the access capabilities to structure/class elements and functions. And in different languages these access modifiers are different. Sometimes they really confuse you when switching between programming languages. For example, in C-like languages the access modifier will be the word private:
C++
class Example {
private int a
private void foo(){}
}Java
public class Bicycle {
private int speed;
private void speedUp(int increment) {
speed += increment;
}
}In other languages, the access modifier can be the capitalization of the field name (if the method/field name is written with a capital letter – it’s public), for example in Go.
Go
type FooBar struct {
privateVar int
PublicVar int
}There are languages where privacy is organized by the presence of _ before the variable name, for example Dart.
Dart
class NewBox extends StatelessWidget {
String _name;
}Python
And there are languages with their own special approach, for example Python. It has its own special approach:
- if you add
__to the beginning of the variable name – this means that the variable is kind of private, but access to it is still available; - if you add
_to the beginning of the variable name – this means that the variable is kind of private, but access to it is still available, but it is only recommended to touch it for technically competent specialists.
That is, all privacy is based exclusively on agreements between developers.
class A:
def _secret(self):
print("This is a method for technically savvy people!")
def _private(self):
print("This is a private method!")