Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

ipython

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?

like image 973
Wilfred Hughes Avatar asked Feb 24 '15 16:02

Wilfred Hughes


People also ask

Why am I getting syntax errors in Python?

Some of the most common causes of syntax errors in Python are: Misspelled reserved keywords. Missing quotes. Missing required spaces.

Why is my else invalid syntax Python?

In the REPL it must be written on the line after your last line of indented code. In Python code in a file, there can't be any other code between the if and the else . You'll see SyntaxError: invalid syntax if you try to write an else statement on its own, or put extra code between the if and the else in a Python file.

How do I fix invalid syntax error in Python?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

How do you fix a syntax error?

How to Fix It: If a syntax error appears, check to make sure that the parentheses are matched up correctly. If one end is missing or lined up incorrectly, then type in the correction and check to make sure that the code can be compiled. Keeping the code as organized as possible also helps.


1 Answers

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='
like image 139
EdChum Avatar answered Sep 29 '22 12:09

EdChum