I have a program that does some calculations for multiple
different values of a certain quantity. I wanted to either
write a second program that would hold the first quantity
fixed and loop over a different one, or to extend the
first to do either of those based on a command-line option.
While noodling around, I thought "what about --" and came up
with the following idea:
============================================================
if options.angle:
angles = [0.05*k for k in range(7)]
else:
comps = [0.1*k for k in range(6)]
for variable in angles if options.angle else comps:
if options.angle:
angle = variable
else:
X_C = variable * -X_L
junk = sys.stdout.write( "%4.2f %4.3f\n" % (angle,X_C) )
# Hard part elided for clarity
============================================================
I was surprised to see that one could actually write a
for-loop like that, although it tickled my inner hacker.
What I'd like to know is: Is this an egregious abuse of the
language, or is it perfectly pythonic?
Opinions, and even violent disagreement, are solicited.
--
Michael F. Stemper
Always use apostrophe's and "quotation marks" properly.
--
https://mail.python.org/mailman3//lists/python-list.python.org