Why does a semicolon return an empty string in IPython?

Question:

When running IPython, if you evaluate a semicolon you get a return value of the empty string, as shown below. Why?

My theory is that it’s IPython stripping out the line terminator in the statement but that still doesn’t explain why it returns a string.

ipython

Output:

Python 2.7.12 (default, Oct 11 2016, 05:24:00)
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

Session:

In [1]: ;
Out[1]: ''

This also works in IPython for Python 3.

This doesn’t work in the standard Python shell, only in IPython.

Asked By: Ben Elgar

||

Answers:

IPython has special handling for semicolons at the start of the line. For example:

In [1]: ;ord A   # autocall -- calls function "ord" on string "A"
Out[1]: 65

It looks like if it can’t parse a function name, you get buggy behavior:

In [4]: ;;;
Out[4]: ';;'

In [5]: ;?huh
Out[5]: '?huh'

In [6]: ;?huh?
Object `huh` not found.

In [7]: ;?huh
Out[7]: '?huh;?huh'

In [8]: 

(The above is with IPython 2.4.1 running Python 3.5.2.)

So I wouldn’t try to interpret it as “stripping out a line terminator” or trying to relate it back to Python syntax.

Answered By: K. A. Buhr
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.