0

So yesterday I tried to use repr and str in my code for printing objects from list. Here just small example code where I run to same problem.

class Something:
    def __init__(self):
        pass

    def __repr__(self):
        return "I want this out"

    def __str__(self):
        return "this comes out"

def main():

    k = Something()
    k
    print(k)

main()

What is printed:

this comes out

Process finished with exit code 0

Why can't i get the repr out of my object even though I have given it line to return when object is called?

Community
  • 1
  • 1
Mkk0
  • 25
  • 4
  • You could [`inspect` who the caller was](https://stackoverflow.com/questions/2654113/python-how-to-get-the-callers-method-name-in-the-called-method) and adapt accordingly – Adelin Jan 19 '18 at 07:08
  • You could always write __str__ to return __repr__.. `def __str__(self): return self.__repr__()` – Wololo Jan 19 '18 at 07:13
  • @Magnus I think OP wants different output based on what method is called – Adelin Jan 19 '18 at 07:13
  • You didn't get it because you never called it... `repr(k)` will give you what you want. – Sraw Jan 19 '18 at 07:15
  • @Adelin I agree. I just wanted to suggest it, but perhaps it was obvious. – Wololo Jan 19 '18 at 07:17
  • Try looking at https://stackoverflow.com/questions/3764360/when-repr-is-called – Adelin Jan 19 '18 at 07:21
  • 2
    Check out this link: https://www.pythoncentral.io/what-is-the-difference-between-__str__-and-__repr__-in-python/ – Poiz Jan 19 '18 at 07:22
  • @Adelin That's not true, if an object contains only `__repr__`, `print` will use `__repr__`. But if an object contains `__str__`, `print` will choose `__str__` first. – Sraw Jan 19 '18 at 07:22
  • 1
    The line with `k` by itself doesn't call `repr`; only expression statements executed *directly* from the REPL do so. – chepner Jan 19 '18 at 07:25
  • Even with repr(k) I only get out the string in __str__(). Really don't get this thing. – Mkk0 Jan 19 '18 at 07:45

2 Answers2

0

__repr__() and __str__() are used for different purposes.

  • __repr__'s goal is to be unambiguous
  • __str__'s goal is to be readable

There is a great write-up on this here.

In your case, your code simply references k in hopes that this will display the repr version of k. This works on an interactive prompt, but does NOT work within a script. Within a script, to see the repr representation of your object, you must use the repr() function. To see the str() representation of your object, you normally must use the str() function.

Notably, the print() function defaults to displaying the str representation, which is why you can print it without explicitly calling str() first.

class Something:
    def __init__(self):
        pass

    def __repr__(self):
        return "I want this out"

    def __str__(self):
        return "this comes out"

def main():

    k = Something()
    print("repr:", repr(k))
    print("str:", str(k))
    print("defaults to calling str() if available: ", k)

main()

repr: I want this out
str: this comes out
defaults to calling str() if available:  this comes out
E. Ducateme
  • 4,028
  • 2
  • 20
  • 30
-1

There are 2 ways your code was run by the interpreter. First is REPL context(iPython shell, or ipdb debug env), in this situation, python interpreter will call __repr__ function, I tried below in a ipython env, it work like this:

In [1]: class Something(object):
   ...:
   ...:     def __repr__(self):
   ...:         return 'in __repr__'
   ...:

In [2]: k = Something()

In [3]: k
Out[3]: in __repr__

Second, when you start your script or project by python xxx.py, the interpreter will call the __str__.

I think you just tried the second way.

Hope this can help you a bit.

guerbai
  • 325
  • 4
  • 11