Answers for "__repr__ in python"

2

__repr__ or __str__

very briefly
 - The default implementation is useless (it’s hard to think of one which wouldn’t be, but yeah)
 - __repr__ goal is to be unambiguous
 - __str__ goal is to be readable
 - Container’s __str__ uses contained objects’ __repr__
 
see source for more info
Posted by: Guest on October-14-2020
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
7

repr() in python

#The repr() function returns a printable representation of the given object.
#repr() takes a single object.
#Syntax
val = "string"
print(repr(val)) #output ---->"'string'"
Posted by: Guest on June-16-2020
1

__repr__ in python

def __repr__(self) -> str:
        return
Posted by: Guest on March-14-2021
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
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

Python Answers by Framework

Browse Popular Code Answers by Language