2

It's nice to be able to enter an object in the shell, and get something back, e.g.,

>>>foo
I am foo

Usually, using print(foo) in a module script will yield the same result, as in the case above (I'm using Python 3.5). But often, with instances of complex classes, you can get wildly different outputs.

This raises the question, what exactly happens when you type an object name and hit enter in the interactive python shell? What built-in is called?

Example:

In module:

print(h5file)

Output:

tutorial1.h5 (File) 'Test file' Last modif.: 'Wed Jun 8 21:18:10 2016' Object Tree: / (RootGroup) 'Test file' /detector (Group) 'Detector information' /detector/readout (Table(0,)) 'Readout example'

Versus shell output

>>>h5file File(filename=tutorial1.h5, title='Test file', mode='w', root_uep='/', filters=Filters(complevel=0, shuffle=False, fletcher32=False, least_significant_digit=None)) / (RootGroup) 'Test file' /detector (Group) 'Detector information' /detector/readout (Table(0,)) 'Readout example' description := { "Country": UInt16Col(shape=(), dflt=0, pos=0), "Geo": UInt16Col(shape=(), dflt=0, pos=1), "HsCode": Int8Col(shape=(), dflt=0, pos=2), "Month": UInt16Col(shape=(), dflt=0, pos=3), "Quantity": UInt16Col(shape=(), dflt=0, pos=4),

Constantino Tsarouhas
  • 6,846
  • 6
  • 43
  • 54
Hexatonic
  • 2,251
  • 2
  • 21
  • 26

4 Answers4

5

print implicitly applies str() to each printed item to obtain a string, while the shell implicitly applies repr() to obtain a string. So it's the difference (if any) between an object's __str__() and __repr__() methods

>>> class A(object):
...    def __str__(self):
...        return "I'm friendly!"
...    def __repr__(self):
...        return "If different, I'm generally more verbose!"

>>> a = A()
>>> print(a)
I'm friendly!
>>> a
If different, I'm generally more verbose!

Note that I'm ignoring the possibility that the shell you use has overridden the default sys.displayhook function.

Tim Peters
  • 67,464
  • 13
  • 126
  • 132
2

In the REPL, it's __repr__() that's used to give you the representation of an object whereas print gives you the result of the __str__() function, as per the following transcript:

pax> python
Python 2.7.9 (default, Jun  1 2016, 16:07:34) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class xyzzy:
...     def __repr__(self):
...         return "me"
...     def __str__(self):
...         return "other-me"
... 
>>> plugh = xyzzy()
>>> plugh
me
>>> print plugh
other-me
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • So to have the same result as the REPL in a module, one would use print(repr(h5file)). That works. Thank you. – Hexatonic Jun 09 '16 at 01:38
  • @Hexatonic, yes, the `repr()` call gives you a string matching what you would get by just entering the object into the REPL. And, of course, `print()` calling `str()` on that string is basically a no-op, giving you the representation. – paxdiablo Jun 09 '16 at 01:45
  • @Hexatonic: FWIW, You can easily get the repr of an object when using the format function or method with `{!r}` instead of `{}` in your format string. – PM 2Ring Jun 09 '16 at 02:08
1

__repr__() is the method called in this situation. It returns the "official" (canonical) representation of the object, as opposed to __str__ which is typically less structured.

Paul Bissex
  • 1,611
  • 1
  • 17
  • 22
1

Compare repr vs str:

>>> class A: pass
... 
>>> A
<class __main__.A at 0x101c609a8>
>>> A()
<__main__.A instance at 0x101cea488>
>>> print A
__main__.A
>>> repr(A)
'<class __main__.A at 0x101c609a8>'
>>> str(A)
'__main__.A'

You can create your own custom __str__ and __repr__ if you wish:

>>> class Foo:
...    def __repr__(self): return 'I am foo'
... 
>>> Foo()
I am foo
dawg
  • 98,345
  • 23
  • 131
  • 206