+ 13
private variables in python
Can anyone pls explain what are single undescore and double underscore variables and how they can be accessed outside of any class?
17 Respostas
+ 15
You can access them just by writing them with the underscores:
print(yourInstance._thatValue)
Python has no privacy. It relies on the convention that programmers *voluntarily* don't touch members that start with _.
+ 8
Single Underscore:
> In Interpreter
>> After a name
>> Before a name
**
*** Double Underscore:
__leading_double_underscore
__before_after__
+ 3
if we want to calculate how much money we will have on the 5th day, we can use this expression: 0.01*(2**5) = 0.32 dollars (multiply the penny by 2 raised to the power of 5).
+ 2
For everyone saying <variable> = None is declaration without initialization, you are wrong. The assignment operator = is there, meaning something is being assigned to the variable, which defines the variable type and initializes it, even if the thing to be assigned was of a None type.
+ 2
Variable with leading "_" single underscore is just a convention to tell that this variable shall not be imported. You can say it's a soft rule(Rules are meant to be broken XD)
Variable with leading '__' double underscore is to tell the interpreter that this variable is not shared and is local to only single class. It's also called name mangling.
+ 1
To declare private variable as __var
And to access this variable as instance name._classname__var
+ 1
If you use a variable with a singel underscore than it is called weakly private, but if you use a variable with doublekunderscore than it is called strongly pirvate
+ 1
I do not understand the question!
+ 1
Single underscore is private
+ 1
Privacy doesn't exist in python but you can use conventions like Double underscore for private, if you use a single underscore it means current class
- 1
I can not
- 1
Op
- 1
ć
¤ć
¤
- 1
Yes, is possible.
- 1
a single underscore at starting is less private and a double underscore at starting is more private
- 1
Hi