-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmethod-overloading-3.py
57 lines (45 loc) · 1011 Bytes
/
method-overloading-3.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
#method-overloading-3.py
# We've seen that inherited methods can be overloaded.
# This is possible using in-built functions as well.
# Let's see how we can overload methods from the `list` module.
class MyList(list):
def __getitem__(self, index):
if index == 0:
raise IndexError
if index > 0:
index -= 1
return list.__getitem__(self, index)
def __setitem__(self, index, value):
if index == 0:
raise IndexError
if index > 0:
index -= 1
list.__setitem__(self, index, value)
x = MyList(["a", "b", "c"])
print("First",x)
print("-" * 10)
x.append("d")
print("Second",x)
print("-" * 10)
x.__setitem__(4, "e")
print("Third",x)
print("-" * 10)
print(x[1])
print("Fourth",x.__getitem__(1))
print("-" * 10)
print(x[4])
print("Fifth",x.__getitem__(4))
'''
O/P-
First ['a', 'b', 'c']
----------
Second ['a', 'b', 'c', 'd']
----------
Third ['a', 'b', 'c', 'e']
----------
a
Fourth a
----------
e
Fifth e
'''