[英]Why is ,,b = 1,2,3 parsed as (',b', '=', '1,2,3') in IPython?
我注意到IPython有一些非常奇怪的解析表現為不合法的Python語法。
In [1]: ,,b = 1,2,3
Out[1]: (',b', '=', '1,2,3')
分號也有類似的東西,但它並沒有分成一個元組。
In [4]: ;;foo = 1;2;3
Out[4]: ';foo = 1;2;3'
雖然它看起來像;
表示該行的其余部分被視為文字字符串,但情況並非總是這樣:
In [5]: ,foo
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-f2137ad20ab5> in <module>()
----> 1 foo("")
NameError: name 'foo' is not defined
In [6]: ;foo
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-6-f2137ad20ab5> in <module>()
----> 1 foo("")
NameError: name 'foo' is not defined
為什么IPython會這樣做? 這是記錄還是可配置的?
這是一種強制引用的便捷方法,請參閱文檔: https : //ipython.readthedocs.io/en/stable/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 [260]:
;
Out[260]:
''
和逗號一樣,
:
In [261]:
,
Out[261]:
''
我無法看到任何允許你覆蓋它的地方,我可能錯了,但它看起來像硬編碼的東西。
編輯
好的我發現了一個關於此的郵件 ,您可以通過添加(或創建,如果它不存在)以下內容將其關閉到.ipython/profile_default/static/custom/custom.js
,這是未經測試的:
if (IPython.CodeCell) {
IPython.CodeCell.options_default.cm_config.autoCloseBrackets = false;
}
關於你的最后一點,為什么,,b = 1,2,3
被區別對待,看起來白色空間正在引入某種中斷,然后將其變成一個元組:
In [9]:
,,b =
Out[9]:
(',b', '=')
比較沒有空格:
In [10]:
,,b=
Out[10]:
',b='
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.