Skip to content Skip to sidebar Skip to footer

Simulate Private Variables In Python

Possible Duplicate: private members in python I've got few variables I really want to hide because they do not belong outside my class. Also all such non-documented variables re

Solution 1:

Private variables is covered in the Python documentation:

9.6. Private Variables

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

Summary: use an underscore before the name.

Solution 2:

From the Python docs:

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

Post a Comment for "Simulate Private Variables In Python"