ipython/ipython 笔记本中函数调用前出现分号的奇怪行为

Weird behaviour with semicolon before function call in ipython/ipython notebook

我偶然发现了一些使用 ipython-notebook 的奇怪行为,想知道目的是什么(如果有的话)。如果您在函数调用之前输入分号,您将获得将函数应用于字符串的结果,该字符串反映了函数名称之后的所有代码。例如,如果我做 ;list('ab') 我得到 list("('ab')") 的结果:

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

我正在使用 jupyteripython 4。它发生在 ipythonipython notebook 中。 有没有人以前见过这个或者有人知道它是否有意为之,如果是这样,为什么?

自动引用函数参数的命令:http://ipython.readthedocs.org/en/latest/interactive/reference.html#automatic-parentheses-and-quotes

来自文档:

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 [279]:
;list(ab)

Out[279]:
['(', 'a', 'b', ')']