Is it possible to have protected class variables or methods in python? Can I see an example of such usage?
-
2You can mimic with underscores and double underscores in start of variable name and method. Just a conventionCaio Belfort– Caio Belfort2019-02-26 00:08:09 +00:00Commented Feb 26, 2019 at 0:08
-
Python classes do not support access modifiers.juanpa.arrivillaga– juanpa.arrivillaga2019-02-26 01:22:15 +00:00Commented Feb 26, 2019 at 1:22
2 Answers
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.
1 Comment
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