在ipython/ipython Notebook中在函数调用之前用分号表示奇怪的行为 [英] Weird behaviour with semicolon before function call in ipython/ipython notebook

查看:75
本文介绍了在ipython/ipython Notebook中在函数调用之前用分号表示奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I stumbled upon some strange behaviour using ipython-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', "'", ')']

我正在将jupyteripython 4一起使用.它发生在ipythonipython notebook中. 有没有人看过这个,或者有没有人知道它是否是预期的,如果是,为什么?

I'm using jupyter with ipython 4. It happens in ipython as well as ipython notebook. Has anyone seen this before or does anyone know if it's intended and, if so, why?

推荐答案

这是用于自动引用函数args的命令:

It's a command for automatic quoting of function args: 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 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', ')']

这篇关于在ipython/ipython Notebook中在函数调用之前用分号表示奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆