0

I've always thought that Jupyter simply printed out the repr of an object, but that is not the case.

Here is an example. If I evaluate this in a notebook:

obj = type(2)
obj

I just get: int.

If I do instead

print(obj)

I get: <class 'int'>.

So: what is the Python instruction to simulate what the notebook does during the evaluation of a variable?

Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90
  • You know Jupyter is open-source, right? – Lightness Races in Orbit Aug 03 '16 at 10:54
  • This has nothing to do with evaluating the variable, it's merely about *representing* the variable. If your question is about how to *replicate* the representation, you should either look up how Jupyter implements it, or specify what kind of representation your require - e.g. having types represented by their class name. – MisterMiyagi Aug 03 '16 at 13:24
  • Maybe this? http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python – OneCricketeer Aug 03 '16 at 13:58

1 Answers1

1

Jupyter/IPython uses a rather complex pretty printer. Concerning your example of int, it has a printer for classes/type.

Basically what it does is it gets the class' name via cls.__qualname__ (py3) or cls.__name__ (py2&3) and the module via cls.__module__, and prints them as <module.name>. For builtins, the module name is silently ignored.

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119