0

I am newbie to python and trying to understand repr method. I have written small piece of code below :

>>> class myobj():
...  def __repr__(self):
...   return "the repr is myobj({})".format(self.__arg)
...  def __init__(self):
...   self.__arg = None
... 
>>> p1 = myobj()
>>> p1
the repr is myobj(None)
>>> print(p1)
the repr is myobj(None)
>>> repr(p1)
'the repr is myobj(None)'
>>> 

Correct me if I am wrong here : when I do p1, it calls "repr" I implemented.

I am not able to understand when I call repr(p1), why I am seeing single quotes around the repr is myobj(None).

Can someone explain? Appreciated

blueWings
  • 71
  • 1
  • 9
  • because it is effectively `repr(repr(p1))` you could assign it to a variable or print it – Joran Beasley Oct 10 '22 at 01:27
  • `repr` returns a string value and [In the Python interactive prompt, if you return a string, it will be displayed with quotes around it, mainly so that you know it's a string.](/a/1482660/15497888). This is as opposed to `print`ing a string which will display the literal value without quotes in the console. – Henry Ecker Oct 10 '22 at 01:30

0 Answers0