Guest User

Untitled

a guest
Jan 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. 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
  2. a variable why will he.
  3. In case if some variable needs to private it is initiated with '__'.
  4. The main concept behind not having any of those logic is because, We used to have those access specifiers so
  5. that out side classes will not be able to use the attribute of a class. But unless the developer will not try to
  6. access a class within another class, They are not going to be used like that.
  7. So default is public anyone can access any method anywhere.
  8.  
  9. But, You can privatize them in a different and more logical way. Using _ or __ .
  10. __ can be used to define private attributes in a class. by default we define all variable names as words,
  11. but if you define a class member starting with __ it becomes private and it can not be accessed directly.
  12. Python defines it’s magic functions as __ at start as well as at the end. eg, __init__ but for private attributes of a class
  13. the name should be mangled as __ only at the beginning of the name of the variable.
  14. e.g
  15. >>> class MyClass:
  16. ... def myPublicMethod(self):
  17. ... print 'public method'
  18. ... def __myPrivateMethod(self):
  19. ... print 'this is private!!'
  20. ...
  21. >>> obj = MyClass()
  22. >>> obj.myPublicMethod()
  23. public method
  24. >>> obj._MyClass__myPrivateMethod()
  25. this is private!!
Add Comment
Please, Sign In to add comment