3

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

2
  • 2
    You can mimic with underscores and double underscores in start of variable name and method. Just a convention Commented Feb 26, 2019 at 0:08
  • Python classes do not support access modifiers. Commented Feb 26, 2019 at 1:22

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

IOW python doesn't have access modifiers.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.