4 September 2018

You can define class variables multiple ways in Python, but they also work differently. Underscore is used for "private" or "protected" variables, altough there are no such definitions in Python. There there are class based variables and instance based variables. See code snippet for clarification.

Source code viewer
  1. class MyClass:
  2.  
  3. """Defining class variables.
  4. """
  5.  
  6. #: Class based public variable.
  7. classVariable = None
  8. #: Class based "Private" variable.
  9. _classVariable = None
  10.  
  11. def __init__(self):
  12. #: Instance based public variable.
  13. self.instanceVariable = None
  14. #: Instance based "Private variable"
  15. self._instanceVariable = None
Programming Language: Python