Included Private variables in Class article of Python (#18803)
* Included Private variables in Class * Removed Typos
This commit is contained in:
committed by
Quincy Larson
parent
04eed15fe5
commit
dbcc177a99
@ -79,4 +79,24 @@ class Complex:
|
||||
x = Complex(3.0, -4.5)
|
||||
>>> x.r, x.i
|
||||
(3.0, -4.5)
|
||||
```
|
||||
```
|
||||
|
||||
#### Private Variables in Class:
|
||||
|
||||
Every programming language which follows OOPs (Object Oriented Programming) concept has some mechanism of data hiding.In python if some variable needs to be hidden from the outside world we use the concept of private variables by using `__` before he variable name.
|
||||
|
||||
for example:
|
||||
```
|
||||
class Foo:
|
||||
def __init__(self):
|
||||
self.__privatenum = 3011
|
||||
|
||||
|
||||
Now if we try to access __privatenum outside the class we will get error:
|
||||
x = Foo()
|
||||
x.__privatenum # gives following error : 'Foo' object has no attribute '__privatenum'
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user