Access Modifiers
Access Modifiers
In Python
SANTOSH VERMA
Public Access
All member variables and methods are public by default in Python. So when you want
to make your member public, you just do nothing.
class Cup:
def __init__(self):
self.color = None
self.content = None
def empty(self):
self.content = None
#Driver Code
redCup = Cup()
redCup.color = "red"
redCup.content = "tea"
redCup.empty()
Protected
Protected member is (in C++ and Java) accessible only from within the
class and it’s subclasses. How to accomplish this in Python?
#Driver Code
class Cup: cup = Cup()
def __init__(self): cup._content = "tea“
print(cup.color, cup._content)
self.color = None
self._content = None # protected variable
Output:
def fill(self, beverage):
None tea
self._content = beverage
def empty(self):
self._content = None
Private
By declaring your data member private you mean, that nobody
should be able to access it from outside the class.
class Peak:
def _single_method(self):
pass
def __double_method(self): # for mangling
pass
class Pyth(Peak):
def __double_method(self): # for mangling
pass