1very briefly
2 - The default implementation is useless (it’s hard to think of one which wouldn’t be, but yeah)
3 - __repr__ goal is to be unambiguous
4 - __str__ goal is to be readable
5 - Container’s __str__ uses contained objects’ __repr__
6
7see source for more info
1
2class Person:
3 name = ""
4 age = 0
5
6 def __init__(self, personName, personAge):
7 self.name = personName
8 self.age = personAge
9
10 def __repr__(self):
11 return {'name':self.name, 'age':self.age}
12
13 def __str__(self):
14 return 'Person(name='+self.name+', age='+str(self.age)+ ')'
15
1#The repr() function returns a printable representation of the given object.
2#repr() takes a single object.
3#Syntax
4val = "string"
5print(repr(val)) #output ---->"'string'"
1# The repr() function returns a printable representational string of the given object.
2>>> var = 'foo'
3>>> print(repr(var))
4"'foo'"
1>>>x=4
2>>>repr(x)
3'4'
4>>>str(x)
5'4'
6>>>y='stringy'
7>>>repr(y)
8"'stringy'"
9>>>str(y)
10'stringy'