0101amt 0101amt - 2 months ago 28
Python Question

Understanding repr( ) function in Python

: evaluatable string representation of an object (can "eval()"
it, meaning it is a string representation that evaluates to a Python

In other words:

>>> x = 'foo'
>>> repr(x)


  1. Why do I get the double quotes when I do
    ? (I don't get them
    when I do

  2. Why do I get
    when I do
    and not x which is the

>>> x = 'foo'
>>> x

So the name x is attached to 'foo' string. When you call for example repr(x) the iterpreter puts 'foo' instead of x and then calls repr('foo').

>>> repr(x)
>>> x.__repr__()

repr actually calls a magic method __repr__ of x, which gives the string containing the representation of the value 'foo' assigned to x. So it returns 'foo' inside the string "" resulting in "'foo'". The idea of repr is to give a string which contains a series of symbols which we can type in the interpreter and get the same value which was sent as an argument to repr.

>>> eval("'foo'")

When we call eval("'foo'"), it's the same as we type 'foo' in the interpreter. It's as we directly type the contents of the outer string "" in the interpreter.

>>> eval('foo')

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'foo' is not defined

If we call eval('foo'), it's the same as we type foo in the interpreter. But there is no foo variable an the exception is raised.

>>> str(x)
>>> x.__str__()

str is just the string representation of the object (remember, x variable refers to 'foo'), so this function returns string.

>>> str(5)

String representation of integer 5 is '5'.

>>> str('foo')

And string representation of string 'foo' is the same string 'foo'.