-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_62 Access Modifiers in Python.py
78 lines (57 loc) · 1.85 KB
/
Day_62 Access Modifiers in Python.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# NOTE: There is no concept of public,private and protected. It is used by people on name of public,private and protected.
# Types of access specifiers:
# (1) Public
# (2) Private
# (3) Protected
# PUBLIC ACCESS SPECIFIERS
# class Employee:
# def __init__(self):
# self.__name = 'Indian'
# a = Employee()
# # print(a.__name) #Cannot be accessed directly
# # Name Mangling
# print(a._Employee__name) # Can be accessed indirectly
# print(a.__dir__())
# class Student:
# # constructor defined
# def __init__(self, age, name):
# self.age = age # public variable
# self.name = name # public variable
# obj = Student(18,"Karan")
# print(obj.age)
# print(obj.name)
# PRIVATE ACCESS SPECIFIERS
# class Student:
# def __init__(self, age, name):
# self.__age = age # An indication of private variable
# def __funName(self): # An indication of private function
# self.y = 34
# print(self.y)
# class Subject(Student):
# def __init__(self, age, name):
# super().__init__(age, name)
# obj = Student(21,"Harry")
# obj1 = Subject(22, "Ron")
# # To access private attributes, you can use name mangling
# print(obj._Student__age)
# obj._Student__funName()
# # For the Subject class object, you can do the same
# print(obj1._Student__age)
# obj1._Student__funName()
# PROTECTED ACCESS SPECIFIERS
class Student:
def __init__(self):
self._name = "Harry"
def _funName(self): # protected method
return "CodeWithHarry"
class Subject(Student): # inherited class
pass
obj = Student()
obj1 = Subject()
print(dir(obj))
# calling by object of Student class
print(obj._name)
print(obj._funName())
# calling by object of Subject class
print(obj1._name)
print(obj1._funName())