for variable in angles if options.angle else comps:
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:
-a angles = [0.05*k for k in range(7)]
else:
-a comps = [0.1*k for k in range(6)]
for variable in angles if options.angle else comps:
-a if options.angle:
-a-a-a angle = variable
-a else:
-a-a-a X_C = variable * -X_L
-a junk = sys.stdout.write( "%4.2f-a %4.3f\n" % (angle,X_C) )
-a # 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.
, is fine, but I think what would be a bit clearer might be,
variables = angles if options.angle else comps
for variable in variables:
.
for variable in angles if options.angle else comps:
Just today, I wrote something similar:
if mode == "file":
storage_context = FileStorage()
elif mode == "database":
storage_context = DatabaseStorage( server )
Michael F. Stemper wrote:
============================================================
if options.angle:
-a-a angles = [0.05*k for k in range(7)]
else:
-a-a comps = [0.1*k for k in range(6)]
for variable in angles if options.angle else comps:
The `angles if options.angle else comps` part evaluates to either `angles` or `comps` depending on the value of `options.angle`.-a It's sometimes called a "ternary operator" because it operates on three values (the condition and two possible results).-a If you're familiar with C or Java, it's similar to `options.angle ? angles : comps` (don't worry if you're not familiar with C/Java; just though that might help explain if you do happen to be).
The `for` loop then iterates over whichever of `angles` or `comps` is the result.-a With parentheses to clarify, it's equivalent to:
```
for variable in (angles if options.angle else comps):
```
Or similar to doing:
```
values = angles if options.angle else comps
for variable in values:
```
Personally, if I were going to use a loop like this, I'd probably write it in one or other of those two forms so that it's clearer what it does for someone reading it.-a Stephan noted that it at least needs the parentheses to work in a list comprehension which is probably because, in comprehensions, `if` is used (without an `else`) to select which items yielded by `for` are to be included.
-a-a if options.angle:
-a-a-a-a angle = variable
-a-a else:
-a-a-a-a X_C = variable * -X_L
As far as I can see, depending on `options.angle`, either `angle` or `X_C` is not defined.-a Unless they're each set to an initial/default value somewhere earlier, the next line is bound to fail.
-a-a junk = sys.stdout.write( "%4.2f-a %4.3f\n" % (angle,X_C) )
Is there a reason not to just use `print("%4.2f-a %4.3f" % (angle,X_C))? Unless `junk` is actually used later, you shouldn't need to assign the return value to anything.
"Michael F. Stemper" <michael.stemper@gmail.com> wrote or quoted:
for variable in angles if options.angle else comps:
, is fine, but I think what would be a bit clearer might be,
variables = angles if options.angle else comps
for variable in variables:
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 65 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 07:59:24 |
| Calls: | 862 |
| Files: | 1,311 |
| D/L today: |
1 files (1,366K bytes) |
| Messages: | 264,936 |