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 ("\n Calling the `+` builtin with both lists" )
28
+ print (my_list_1 + my_list_2 )
29
+
30
+ print ("\n Calling `__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
+ '''
0 commit comments