Weird behaviour with semicolon before function call in IPython and Jupyter Notebook

Question:

I stumbled upon some strange behaviour using Jupyter Notebook and wondered what, if any, the purpose was. If you enter a semicolon before a function call, you get the result of applying the function to a string which reflects all the code after the function name. For example, if I do ;list('ab') I get the result of list("('ab')") :

In [138]:    ;list('ab')
Out[138]:
['(', "'", 'a', 'b', "'", ')']

I’m using Jupyter with IPython 4. It happens in IPython as well as Jupyter Notebook.

Is it intended and, if so, why?

Asked By: JoeCondron

||

Answers:

It’s a command for automatic quoting of function arguments: Automatic parentheses and quotes

From the documentation:

You can force automatic quoting of a function’s arguments by using ,
or ; as the first character of a line. For example:

In [1]: ,my_function /home/me  # becomes my_function("/home/me")

If you use ‘;’ the whole argument is quoted as a single string, while
‘,’ splits on whitespace:

In [2]: ,my_function a b c    # becomes my_function("a","b","c")

In [3]: ;my_function a b c    # becomes my_function("a b c")

Note that the ‘,’ or ‘;’ MUST be the first character on the line! This
won’t work:

In [4]: x = ,my_function /home/me # syntax error

In your case, it’s quoting all characters, including ' and ( and ).

You get similar output here, but without the single quotes:

In [279]:
;list(ab)

Out[279]:
['(', 'a', 'b', ')']
Answered By: EdChum
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.