-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path25. Access Specifiers or Access Modifiers.py
47 lines (41 loc) · 1.38 KB
/
25. Access Specifiers or Access Modifiers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'''
Access specifiers or access modifiers in python programming are used to limit the access of class variables and class methods outside
of class while implementing the concepts of inheritance.
Three Types:-
Public access modifier
Private access modifier
Protected access modifier
'''
# Public
class Student:
def __init__(self,roll_no, name, graduation):
self.roll_no= roll_no
self.name= name
self.graduation= graduation
def info(self):
print(f"Roll No. {self.roll_no} name {self.name} has a {self.graduation} degree")
x= Student(10, "Saurabh Singh Bhandari", "BTech")
x.info()
# Private
class Student:
def __init__(self,roll_no, name, graduation):
self.roll_no= roll_no
self.__name= name
self.graduation= graduation
def info(self):
print(f"Roll No. {self.roll_no} name {self.__name} has a {self.graduation} degree")
x= Student(10, "Saurabh Singh Bhandari", "BTech")
x.info()
print(x._Student__name) # Name Mangling
# Protected Access Modifier
class Student:
def __init__(self,roll_no, name, graduation):
self.roll_no= roll_no
self._name= name
self.graduation= graduation
def info(self):
print(f"Roll No. {self.roll_no} name {self._name} has a {self.graduation} degree")
x= Student(10, "Saurabh Singh Bhandari", "BTech")
x.info()
print(x._name) # Name Mangling
print(x.__dir__())