Last active
January 11, 2018 12:33
-
-
Save MishraKhushbu/7f09c8a05fbbc06fa9605da9fcd910ee to your computer and use it in GitHub Desktop.
Access_Specifiers_Python_11_Jan_2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In Python access specifiers doesn't exist.Beacause it's considered as we all are adults here I mean if developerdoesn't want to aceess | |
a variable why will he. | |
In case if some variable needs to private it is initiated with '__'. | |
The main concept behind not having any of those logic is because, We used to have those access specifiers so | |
that out side classes will not be able to use the attribute of a class. But unless the developer will not try to | |
access a class within another class, They are not going to be used like that. | |
So default is public anyone can access any method anywhere. | |
But, You can privatize them in a different and more logical way. Using _ or __ . | |
__ can be used to define private attributes in a class. by default we define all variable names as words, | |
but if you define a class member starting with __ it becomes private and it can not be accessed directly. | |
Python defines it’s magic functions as __ at start as well as at the end. eg, __init__ but for private attributes of a class | |
the name should be mangled as __ only at the beginning of the name of the variable. | |
e.g | |
>>> class MyClass: | |
... def myPublicMethod(self): | |
... print 'public method' | |
... def __myPrivateMethod(self): | |
... print 'this is private!!' | |
... | |
>>> obj = MyClass() | |
>>> obj.myPublicMethod() | |
public method | |
>>> obj._MyClass__myPrivateMethod() | |
this is private!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment