Skip to content

Commit 5f4382c

Browse files
committed
Methods
1 parent bf2e4e9 commit 5f4382c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Diff for: Methods/magic-method.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# In the backend, python is mostly objects and method
2+
# calls on objects.
3+
4+
# Here we see an example, where the `print()` function
5+
# is just a call to the magic method `__repr__()`.
6+
7+
8+
class PrintList(object):
9+
def __init__(self, my_list):
10+
self.mylist = my_list
11+
12+
def __repr__(self):
13+
return str(self.mylist)
14+
15+
16+
printlist = PrintList(["a", "b", "c"])
17+
print(printlist.__repr__()) #['a', 'b', 'c']
18+
19+
20+
# To read more on magic methods, refer :
21+
# http://www.rafekettler.com/magicmethods.html
22+
23+
my_list_1 = ["a", "b", "c"]
24+
25+
my_list_2 = ["d", "e", "f"]
26+
27+
print("\nCalling the `+` builtin with both lists")
28+
print(my_list_1 + my_list_2)
29+
30+
print("\nCalling `__add__()` with both lists")
31+
print(my_list_1.__add__(my_list_2))
32+
'''
33+
O/P-
34+
Calling the `+` builtin with both lists
35+
['a', 'b', 'c', 'd', 'e', 'f']
36+
37+
Calling `__add__()` with both lists
38+
['a', 'b', 'c', 'd', 'e', 'f']
39+
'''

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class myClass():
6767
* Instance Method
6868
* Class Method
6969
* Static Method
70+
71+
*We have one more method called [magic method](Methods/magic-method.py)
72+
7073
------------
7174
------------
7275
#### 03. Objects

0 commit comments

Comments
 (0)