-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46_accessmodifiers.py
More file actions
41 lines (31 loc) · 1.08 KB
/
46_accessmodifiers.py
File metadata and controls
41 lines (31 loc) · 1.08 KB
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
class Student:
# constructor is defined
def __init__(self, age, name):
self.age = age # public variable
self.name = name # public variable
obj = Student(21,"Harry")
print(obj.age)
print(obj.name)
class MyClass:
def __init__(self):
self._nonmangled_attribute = "I am a nonmangled attribute"
self.__mangled_attribute = "I am a mangled attribute"
my_object = MyClass()
print(my_object._nonmangled_attribute) # Output: I am a nonmangled attribute
print(my_object.__mangled_attribute) # Throws an AttributeError
print(my_object._MyClass__mangled_attribute) # Output: I am a mangled attribute
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()
# calling by object of Student class
print(obj._name)
print(obj._funName())
# calling by object of Subject class
print(obj1._name)
print(obj1._funName())