我偶然发现了一些使用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:
In [1]: ,my_function /home/me # becomes my_function("/home/me")You can force automatic quoting of a function’s arguments by using , or ; as the first character of a line. For example:
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")If you use ‘;’ the whole argument is quoted as a single string, while ‘,’ splits on whitespace:
In [4]: x = ,my_function /home/me # syntax errorNote that the ‘,’ or ‘;’ MUST be the first character on the line! This won’t work:
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', ')']更多推荐
发布评论