Is display() built in, i.e. a predefined global?

The docs speak of ‘display()’ as being something other worldly that would be found in another namespace:


https://jupyterlab.readthedocs.io/en/stable/user/file_formats.html?highlight=display

But it is available without importing anything:

<error insufficient points to allow a second image inclusion in post – jupyterbot>
Screenshot from 2021-11-11 21-32-49|690x209
(sin() shows the expected behavior as the function has not been imported)

Is display() built in? The IPython line from the docs is redundant? Is ‘display’ now of the same rank as predefined global functions such as ‘print()’ ?

This is an interesting question. The answer for the first question is easy to check in IPython terminal:

> 'display' in dir(__builtin__)
True

Why? According to the IPython changelog:

The display() function is now available by default in an IPython session, meaning users can call it on any object to see their rich representation. This should allow for better interactivity both at the REPL and in notebook environment.

Which also explains why that line is not redundant in certain applications:

Scripts and library that rely on display and may be run outside of IPython still need to import the display function using from IPython.display import display.

How? It looks like IPython modifies builtins by injecting display and __IPYTHON__ constant:

When? Since version 5.0 so for quite a while now. See Inject display into builtins by Carreau · Pull Request #10596 · ipython/ipython · GitHub for more context.

2 Likes