A
A
AlexBoss2019-05-06 11:43:38
Python
AlexBoss, 2019-05-06 11:43:38

What is the meaning of single and double underscore before a variable or method when defining a class?

Good afternoon. I just can't figure out the meaning of the single underscore.
They write that it adds privacy, but I can still refer to all objects:
_internal_name = 'one_nodule' # private variable
_internal_version = '1.0' # private variable
class _Base: # private class
_hidden_factor = 2 # private variable
def __init__(self, price ):
self._price = price
def _double_price(self): # private method
return self._price * self._hidden_factor
def get_double_price(self):
return self._double_price()
Regarding the double, could you clarify. I realized that the _ClassName__method is being decoded, but I don’t quite understand how it looks in practice
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2019-05-06
@AlexBoss

Example An
attempt to call a private method results in an error.
5ccff785271f7736875241.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question