我正在Python交互式Shell(在Windows XP下的ActiveState ActivePython 2.6.4)中工作。我創建了一個我想要的功能。但是,我清除了屏幕,因此我無法返回查看功能定義。它也是一個多行功能,所以重新顯示行的向上箭頭的價值很小。反正有返回函數的實際代碼嗎?有「代碼」和「dir()顯示」func_code「屬性,但我不知道它們是否包含我需要的。在交互式shell中顯示函數定義
回答
不,__code__
和func_code
是對編譯字節碼的引用 - 您可以反彙編它們(請參閱dis.dis
),但不能回到Python源代碼。
唉,源代碼是簡單地走了,不隨地想起...:
>>> import inspect
>>> def f():
... print 'ciao'
...
>>> inspect.getsource(f)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 694, in getsource
lines, lnum = getsourcelines(object)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 683, in getsourcelines
lines, lnum = findsource(object)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 531, in findsource
raise IOError('could not get source code')
IOError: could not get source code
>>>
如果inspect
不能得到它,這是一個漂亮的指示標誌。
如果你是使用GNU readline
的平臺上(基本上,除了任何Windows)中,你可能會利用這一事實本身readline
不記得一些「歷史」,可以寫出來的文件... :
>>> readline.write_history_file('/tmp/hist.txt')
然後閱讀該歷史文件 - 但是,我知道在Windows中無法做到這一點。
您可能希望使用一些具有更好內存功能的IDE,而不是「原始」命令解釋程序,尤其是在諸如Windows的平臺上。
我同意IDE。但是shell看起來像一個DOS命令窗口,PHB並不問我在做什麼。 – 2010-02-03 16:11:06
也許這是一個很好的功能請求空閒... – 2010-02-03 16:12:04
ipython可能有助於豐富的IDE在DOS命令窗口,請參閱http://ipython.scipy.org/moin/ - 它是如此豐富,我失去了軌道現在大部分功能;-)。 – 2010-02-03 16:38:57
除非在activestate shell上有這樣做的方法,否則沒有辦法檢索您在shell中鍵入的確切代碼。至少在Linux上,使用由CPython提供的Python Shell,沒有特殊的方法來實現這一點。也許使用iPython。
func_code屬性是一個表示函數字節碼的對象,你可以從這個對象得到的唯一東西是字節碼本身,而不是「原始」代碼。
在Linux上,'readline.write_history_file'會有所幫助 - 但不能在Windows上。 – 2010-02-03 16:07:45
不,不是真的。您可以將yourfunc.func_code.co_code(實際編譯的字節碼)寫入文件,然後嘗試使用decompyle或unpyc對它們進行反編譯,但兩個項目都很舊且無法維護,並且從不支持反編譯。
簡單地將函數寫入文件以開始使用它是非常容易的。
它已經很長時間了,但可能有人需要它。 getsource
給出的源代碼:
>>> def sum(a, b, c):
... # sum of three number
... return a+b+c
...
>>> from dill.source import getsource
>>>
>>> getsource(sum)
'def sum(a, b, c):\n # sum of three number\n return a+b+c\n'
安裝dill
運行pip install dill
。要得到它的格式:
>>> fun = getsource(sum).replace("\t", " ").split("\n")
>>> for line in fun:
... print line
...
def sum(a, b, c):
# sum of three number
return a+b+c
- 1. iOS自定義按鈕不顯示爲交互式通知
- 2. 擴展Python交互式shell
- 3. 交互式PHP shell技巧
- 4. 完成JavaScript交互式shell
- 5. 使用phpseclib顯示超過17行交互式shell
- 6. 登錄shell和交互式shell?
- 7. Bash交互式和非交互式shell行爲
- 8. 顯示函數定義
- 9. 自定義交互式外殼
- 10. F#交互式CsvProvider未定義
- 11. C++簡單的交互式shell提示在重定向輸入時隱藏
- 12. Octave在交互模式下顯示gnuplot,但不在命令行中顯示
- 13. SCIP:通過交互式shell解決QP
- 14. 通過PHP的交互式shell命令
- 15. java Runtime.exec運行交互式shell掛起
- 16. F#交互式shell(fsi)教程?
- 17. 交互式shell腳本:遞歸太深
- 18. 在bash中定義一個沒有shell腳本的shell函數?
- 19. 在PHP中運行交互Shell腳本
- 20. 交互式Ruby子shell有什麼意義?
- 21. 以其他用戶的名義運行交互式shell腳本
- 22. 如何在Linux上的窗口中顯示交互式SVG?
- 23. F# - 在F#交互式窗口中顯示完整結果
- 24. 交互式TCL外殼在cygwin中沒有顯示輸出
- 25. 無法在GAE燒瓶應用中包含交互式python shell
- 26. Django在交互式shell中被忽略的字段
- 27. 如何在Google datalab容器中獲得交互式shell?
- 28. 在CLI模式下從PHP運行交互式shell命令
- 29. 如何檢查交互式shell中表達式的邏輯值?
- 30. Angular nvd3交互式指南顯示太多數據
我曾建議使用「幫助」功能,但後來意識到你想要的功能,而不僅僅是它的簽名的實際代碼。 – 2010-02-03 16:24:22
我想獲得我在控制檯中編寫的所有代碼作爲文件,但至於收到的答案是不可能的...... sad – 2012-07-15 22:19:59