Why is ,,b = 1,2,3 parsed as (',b', '=', '1,2,3') in IPython?

ipython, python

Solution

It's a convenience method for forcing the quotation, see the docs: https://ipython.readthedocs.io/en/stable/interactive/reference.html#automatic-parentheses-and-quotes

From the docs:

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

For example just `;` outputs an empty string:

In [260]:

;
Out[260]:
''

As does just a comma `,`:

In [261]:

,
Out[261]:
''

I can't see anywhere that allows you to override this, I may be wrong but it looks like something that is hard coded in.

EDIT

OK I found a mail post about this, you can turn it off by adding (or creating if it doesn't exist) the following to `.ipython/profile_default/static/custom/custom.js`, this is untested:

if (IPython.CodeCell) {
    IPython.CodeCell.options_default.cm_config.autoCloseBrackets = false;
}

Regarding your last point about why `,,b = 1,2,3` is being treated differently it looks like the white space is introducing some kind of break which then turns this into a tuple:

In [9]:

,,b =

Out[9]:
(',b', '=')

compare with no spaces:

In [10]:

,,b=
Out[10]:
',b='

Problem

I've noticed that IPython has some very strange parsing behaving for syntax that isn't legal Python. ``` In [1]: ,,b = 1,2,3 Out[1]: (',b', '=', '1,2,3') ``` There's something similar going on with semicolons, but it's not splitting into a tuple. ``` In [4]: ;;foo = 1;2;3 Out[4]: ';foo = 1;2;3' ``` Whilst it looks like `;` means the rest of the line is treated as a literal string, this isn't always the case: ``` In [5]: ,foo --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-5-f2137ad20ab5> in <module>() ----> 1 foo("") NameError: name 'foo' is not defined In [6]: ;foo --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-6-f2137ad20ab5> in <module>() ----> 1 foo("") NameError: name 'foo' is not defined ``` Why does IPython do this? Is this documented or configurable?

Original source