; as ("") in JupyterLab, bug?

41 views
Skip to first unread message

Gavin Scott

unread,
Oct 29, 2020, 4:53:54 PM10/29/20
to Project Jupyter
If I execute a cell containing only a semicolon in 2.2.6 JupyterLab and 3.8.5 Anaconda Python, I get back an empty string which seems odd. If I try two semicolons I get an error that suggests each one is getting replaced by ("")

<>:1: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma? <>:1: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma? <ipython-input-67-72f854f54d1b>:1: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma? ("")("")
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-67-72f854f54d1b> in <module> ----> 1 ("")("") TypeError: 'str' object is not callable

The python REPL rejects these as SyntaxErrors. Bug? Or some obscure Jupyter thing?

Robert Schroll

unread,
Oct 30, 2020, 2:42:20 PM10/30/20
to jup...@googlegroups.com, Project Jupyter
On Oct 29 2020, at 1:53 pm, Gavin Scott <ga...@learn.bio> wrote:
If I execute a cell containing only a semicolon in 2.2.6 JupyterLab and 3.8.5 Anaconda Python, I get back an empty string which seems odd. If I try two semicolons I get an error that suggests each one is getting replaced by ("")

[snip]

The python REPL rejects these as SyntaxErrors. Bug? Or some obscure Jupyter thing?

This behavior is being caused by the IPython kernel.  (I can see the same behavior in the IPython repl.)  IPython contains several features for automatically calling functions and quoting arguments [1].  Specifically, if a line begins with a semicolon, the first word is treated as a callable, and the rest of the line is quoted as a single argument.  That is, the entry
;func some args
gets transformed to
func('some args')

In  a little testing, it appears that if no string follows the callable name, the empty string is used as a default.  Thus
;func
becomes
func('')
Moreover, if there is whitespace following the semicolon, an identity function is assumed.  Thus,
; a b
becomes
(lambda x: x)('a b')
which returns
'a b'

My guess, then, is that
;
becomes
(lambda x: x)('')
returning
''
However, the entry
;;
doesn't have whitespace following the first semicolon, so the identity function behavior is not invoked.  The second semicolon marks the end of the statement, so IPython tries to divide the text between there ('') into a callable name and an argument.  It uses the empty string for the function name, and then fills in the empty string default argument, and tries to execute
('')('')
giving the error you note. Arguably, this is a bug, but it's hard to say what the "correct" behavior should be.

Hope that helps,
Robert

Reply all
Reply to author
Forward
0 new messages