3

Is it possible to have protected class variables or methods in python? Can I see an example of such usage?

surendrapanday
  • 530
  • 3
  • 13

2 Answers2

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