Access Specifiers in Python:
The meaning of Access specifiers means that the way or how the variables or the members of the class is accessed or used outside that class and in what manner. It can be either public, private or protected.
So let's get into these things more deeply........
TYPES OF ACCESS SPECIFIERS :-
1. public :- public members are the objects/attributes in side the class which can be accessed from inside the class as well as outside the class, which means they can be accessed by anyone and even the user can change the value of that variable during run time. In python we don't have any keyword to specify the public member of the class, in this language every member is set to public by default.
2. private :- private members are those members of the class which cannot be accessed from outside the class. The values of the members or variables that are in private can not be changed during run time too. Any attempt to change the value or to do any such things to the private members of the class will lead to an 'Attribute Error'. For private also python don't have any keyword to specify that's why to denote private members we us '__' (double underscore) followed by variable/attribute name and etc.
3. protected :- protected member are those members of the class that can be accessed within the class but not from outside but its accessible to all its sub-classes or inherited classes. The values of the variables or the members can also be modified during run time. Like other access specifiers it also don't have any specific keyword and hence '_' (single underscore) is used to specify the protected members of the class.
EXAMPLES :-
class Student: schoolName = 'Helpforcoders' # class attribute def __init__(self, name, age): self.name=name # instance attribute self.age=age # instance attribute }
class Student: __schoolName = 'Helpforcoders' # private class attribute def __init__(self, name, age): self.__name=name # private instance attribute self.__salary=age # private instance attribute def __display(self): # private method print('This is private method.') }
class Student: _schoolName = 'Helpforcoders' # protected class attribute def __init__(self, name, age): self._name=name # protected instance attribute self._age=age # protected instance attribute }
It's very Helpful 😃👍
ReplyDeletePost a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP