Is it possible to have protected class variables or methods in python? Can I see an example of such usage?
Asked
Active
Viewed 1,559 times
3
-
2You can mimic with underscores and double underscores in start of variable name and method. Just a convention – Caio Belfort Feb 26 '19 at 00:08
-
Python classes do not support access modifiers. – juanpa.arrivillaga Feb 26 '19 at 01:22
2 Answers
4
The short answer is "no." There are conventions and good style that allow you to indicate that someone shouldn't be modifying those variables or calling those methods from outside the class but there is no way to strictly enforce this. There essentially is no such thing as strictly enforced private or protected variables or methods in Python.
See this tutorial.

John
- 1,837
- 1
- 8
- 12
2
No it is not possible. People generally use underscores as a convention for private members.
This question on the general python convention can give some more information.
Python "private" function coding convention
Basically putting a '_' before your member name will indicate to the outside world that it is private.
not_private = 0
_private = 1

Mr. S
- 126
- 6