Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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!!
Add Comment
Please, Sign In to add comment