PY
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
class Testing:
def __init__(self):
self.name = 'this is a Testing object'
def __str__(self):
return 'str: ' + self.name
def __repr__(self):
return 'repr: ' + self.name
# testing diference between __str__ and __repr__:
obj = Testing()
print(obj) # the print function calls the __str__ method of obj
s = '{}'.format(obj) # the format method also calls __str__
print(s)
# if you're in the interactive console and just write
# obj
# it will call the __repr__ method
# you can also use the builtin "repr" function
print(repr(obj))
# the documentation says:
# object.__repr__(self)
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run