Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #197790
| From | "Michael F. Stemper" <michael.stemper@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Hack or beauty? |
| Date | 2026-05-14 08:06 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <10u4hbv$d914$1@dont-email.me> (permalink) |
| References | <10u2hla$2tnlr$1@dont-email.me> <10u2oac$2vstp$1@dont-email.me> |
On 13/05/2026 15.52, Mark Bourne wrote:
> Michael F. Stemper wrote:
>> ============================================================
>> 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:
>
> The `angles if options.angle else comps` part evaluates to either `angles` or `comps` depending on the value of `options.angle`. It's sometimes called a "ternary operator" because it operates on three values (the condition and two possible results). 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).
I'm familiar with that notation from C. I didn't know that it was also in
Java, but that's due to my low level of Java knowledge. But, this comparison
certainly helps.
> The `for` loop then iterates over whichever of `angles` or `comps` is the result. With parentheses to clarify, it's equivalent to:
> ```
> for variable in (angles if options.angle else comps):
> ```
Yeah, I should probably at least parenthesize it.
> 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. 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.
>
>> if options.angle:
>> angle = variable
>> else:
>> X_C = variable * -X_L
>
> As far as I can see, depending on `options.angle`, either `angle` or `X_C` is not defined. Unless they're each set to an initial/default value somewhere earlier, the next line is bound to fail.
I only pasted the tiny portion of the program that was relevant to
my question. I skipped the first 140+ lines, including getting both
of those (and a bunch of other stuff) from an xml file.
>> junk = sys.stdout.write( "%4.2f %4.3f\n" % (angle,X_C) )
>
> Is there a reason not to just use `print("%4.2f %4.3f" % (angle,X_C))? Unless `junk` is actually used later, you shouldn't need to assign the return value to anything.
No, junk is just that -- junk. I adopted this idiom a few weeks after
the write() function started returning a value. I often paste some
code into REPL for test/debug. Having the function value output,
interleaved with real output was quite confusing -- especially in
cases where at program has multiple invocations of write() to produce
a single line of output. Assigning the function output to a variable
nicely supresses its display in REPL.
Thanks,
--
Michael F. Stemper
If you take cranberries and stew them like applesauce they taste much
more like prunes than rhubarb does.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
Hack or beauty? "Michael F. Stemper" <michael.stemper@gmail.com> - 2026-05-13 13:58 -0500
Re: Hack or beauty? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-05-13 19:33 +0000
Re: Hack or beauty? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-05-13 21:04 +0000
Re: Hack or beauty? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-05-14 01:06 +0000
Re: Hack or beauty? "Michael F. Stemper" <michael.stemper@gmail.com> - 2026-05-14 13:25 -0500
Re: Hack or beauty? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2026-05-13 21:52 +0100
Re: Hack or beauty? "Michael F. Stemper" <michael.stemper@gmail.com> - 2026-05-14 08:06 -0500
Re: Hack or beauty? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-05-13 21:37 +0000
csiph-web