We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent bf2e4e9 commit 5f4382cCopy full SHA for 5f4382c
Methods/magic-method.py
@@ -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
39
README.md
@@ -67,6 +67,9 @@ class myClass():
67
* Instance Method
68
* Class Method
69
* Static Method
70
71
+ *We have one more method called [magic method](Methods/magic-method.py)
72
73
------------
74
75
#### 03. Objects
0 commit comments