所以我写一些Python脚本来帮助我做一些简单的计算:Python分配 - 打印语句?
WireRadius = .455/1000/2 #m, 25AWG
CoilInnerRadius = 10.0/1000/2 #m
CoilOuterRadius = 20.0/1000/2 #m
CoilLength = 20.0/1000 #m
CoilVolume = 3.14 * (CoilOuterRadius**2 - CoilInnerRadius**2) * CoilLength #m^3
print "CoilVolume: " + str(CoilVolume)
WireCrossSection = 3.14 * WireRadius**2 #m^2
print "WireCrossSection: " + str(WireCrossSection)
LengthOfWire = CoilVolume/WireCrossSection/2 #m
print "LengthOfWire: " + str(LengthOfWire)
现在,我希望脚本打印出所有的中间部件,这样我就可以看到是怎么回事。如果我搞砸了,这也让我找准线在我的数学是错误的,因为这时候的数字变得无意义。
但是,这显然不是很干,因为我写了每个变量名不是一次,两次,而是三次:
LengthOfWire = CoilVolume/WireCrossSection/2 #m
print "LengthOfWire: " + str(LengthOfWire)
如果我打字到这个交互shell,它会自动吐出中间部件的值回我:
>>> LengthOfWire = CoilVolume/WireCrossSection/2 #m
14.491003502
这是相当不错的,因为赋值语句被保留的意思我知道下一个值是什么。然而,把它在交互shell的问题是,更改并重新运行整个脚本(这是几十个计算长)是乏味的。有没有什么办法可以在通过python script.py运行的脚本中实现这个功能?