Answers for "what is __repr__ in python"

3

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)+ ')'
Posted by: Guest on March-08-2020
6

python repr

# The repr() function returns a printable representational string of the given object.
>>> var = 'foo'
>>> print(repr(var))
"'foo'"
Posted by: Guest on March-26-2020
1

python __repr__ meaning

>>>x=4
>>>repr(x)
'4'
>>>str(x)
'4'
>>>y='stringy'
>>>repr(y)
"'stringy'"
>>>str(y)
'stringy'
Posted by: Guest on April-06-2020
0

python __repr__

import datetime
now = datetime.datetime.now()
now.__str__()
#>>> '2020-12-27 22:28:00.324317'
now.__repr__()
#>>> 'datetime.datetime(2020, 12, 27, 22, 28, 0, 324317)'
Posted by: Guest on April-19-2021
0

python __repr__ meaning

>>>repr(y)
"'a string'"
>>>y2=eval(repr(y))
>>>y==y2
True
Posted by: Guest on April-06-2020

Python Answers by Framework

Browse Popular Code Answers by Language