我喜歡Literate CoffeeScript的想法,並想知道我是否可以使用Python進行一些工作。我試圖完成類似於這個簡單的Literate Ruby的事情,最終得到以下結果。該程序在標準輸入,文件或文件列表中使用Literate Python,並只執行代碼。Literate Python
import fileinput
import re
code = ''
for line in fileinput.input():
match = re.match(r'([ ]{4}|\t)(.*)', line)
if match:
code += match.group(2) + '\n'
exec code
一個簡單的Literate Python文件。
# Python Hello World Program
A simple example of a Literate Python Hello Word program.
print "hello world"
我是新來的Python,想知道我是否遺漏了一些東西,或者是否有更好的解決方案。
'exec'就像'eval'是邪惡的! – zmo
@zmo這實際上是'exec'完全合法的用例。 –
這個問題確實屬於[代碼評論](http://codereview.stackexchange.com/),因爲它是有效的代碼,你在問一些總體的改進。 –