在ipython / ipython笔记本中调用函数前使用分号的奇怪行为(Weird behaviour with semicolon before function call in ipython/

编程入门 行业动态 更新时间:2024-10-26 07:30:35
在ipython / ipython笔记本中调用函数前使用分号的奇怪行为(Weird behaviour with semicolon before function call in ipython/ipython notebook)

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

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

我正在使用jupyter和ipython 4 。 它发生在ipython以及ipython notebook 。 有没有人见过这个或有没有人知道它是否有意,如果有,为什么?

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', "'", ')']

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的命令: http : //ipython.readthedocs.org/en/latest/interactive/reference.html#automatic-parentheses-and-quotes

来自文档:

您可以使用或强制自动引用函数的参数; 作为一行的第一个字符。 例如:

In [1]: ,my_function /home/me # becomes my_function("/home/me")

如果你使用';' 整个参数被引用为单个字符串,而','在空格上分割:

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")

请注意','或';' 必须是线上的第一个角色! 这不起作用:

In [4]: x = ,my_function /home/me # syntax error

在你的情况下,它引用所有字符,包括'和(和)

你在这里得到类似的输出,但没有单引号:

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

It's a command for automatic quoting of function args: http://ipython.readthedocs.org/en/latest/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

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', ')']

更多推荐

本文发布于:2023-07-30 01:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321378.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分号   函数   奇怪   笔记本   ipython

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!