what is __repr__ in python code example
Example 1: what is repr function in python
Upvote = "Do make an upvote click on the top right corner button."
>>> print(Upvote)
>>> 'Do make an upvote click on the top right corner button.'
>>> print(repr(Upvote))
>>>"'Do make an upvote click on the top right corner button.'"
Example 2: python __repr__
import datetime
now = datetime.datetime.now()
now.__str__()
now.__repr__()
Example 3: __repr__ in python
def __repr__(self) -> str:
return
Example 4: python repr
>>> var = 'foo'
>>> print(repr(var))
"'foo'"
Example 5: python __repr__ meaning
>>>x=4
>>>repr(x)
'4'
>>>str(x)
'4'
>>>y='stringy'
>>>repr(y)
"'stringy'"
>>>str(y)
'stringy'
Example 6: python __repr__
class Person:
name = ""
age = 0
def __init__(self, personName, personAge):
self.name = personName
self.age = personAge
def __repr__(self):
return {'name':self.name, 'age':self.age}
def __str__(self):
return 'Person(name='+self.name+', age='+str(self.age)+ ')'