2

Just out of curiosity. I wonder if it is possible to make multiple assignments with the ternary operator in Python. I was thinking of something like this

rval = m if (fl*fm) < 0 else lval = m

On the other hand I wonder why it is not possible to write the code as above. Why wouldn't one implement the Syntax this way? (Sorry if this question is too meta)

EDIT:

to clarify. I was just wrting some bisection function https://en.wikipedia.org/wiki/Bisection_method:

while(n_iterations < n_iters_max):
    m = (lival + rival)/2.0
    fm = f(m)
    if (fl*fm) < 0:
        rival = m
    else:
        lival = m

    if np.abs(rival-lival) < ival_size:
        break

    n_iterations+=1

Thanks for any ideas!

user2853437
  • 645
  • 8
  • 26
  • Since that is *not* valid, it's unclear what you *want* it to mean. – Kelly Bundy Feb 11 '20 at 23:28
  • 1
    That syntax seems confusing. Starting with `rval =` would suggest setting the variable `rval` but that wouldn't be the case if your condition isn't met. That is, `lval` is set instead. – busybear Feb 11 '20 at 23:28
  • sure it is no vlaid code, but I wonder if there is a way to realise my example. like using sets in this article https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator second Post. is kind of out of the box thinking in my opinion. – user2853437 Feb 11 '20 at 23:30
  • Like I said: It's unclear what you want it to mean. So how are we supposed to tell a way to realise what you want if we don't know what you want? – Kelly Bundy Feb 11 '20 at 23:32
  • @HeapOverflow Sorry. Does the Edit clarify what I was thinking about? – user2853437 Feb 11 '20 at 23:43

2 Answers2

2

You could do this:

(rval := m) if (fl*fm) < 0 else (lval := m)

But the normal way with an if-else statement is clearer (at least for now, while we're still new to the := operator) and more appropriate (since you're assigning here, which better is a normal statement than an expression with side effects).

Kelly Bundy
  • 5,629
  • 1
  • 5
  • 31
2

The Zen of Python

While the thought behind the question should be genuinely appreciated and applauded, along with the clever and bleeding edge solution by @Heap Overflow - this seems a dangerous path to travel.

You're right, I'm not the 'Python Police'; however the Zen of Python speaks for itself regarding this implementation.

Explicit is better than implicit.
Simple is better than complex.
Readability counts.
There should be one-- and preferably only one --obvious way to do it.
If the implementation is hard to explain, it's a bad idea.

To make this post an answer rather than a rambling, I'll refer to the quote above. Although the thought is certainly interesting ...

"It's a bad idea" in Python.

S3DEV
  • 4,857
  • 3
  • 14
  • 27
  • @HeapOverflow @s3dev I was kind of aware of the Zen, when asking the question, but readability is kind of ambigous to me. Since tenary operator is a shorthand, it already makes it 'more difficult' to read. The question would more relate to `There should be one-- and preferably only one --obvious way to do it.` So I wonder when would one use the `:=` operator with a tenary operator? – user2853437 Feb 12 '20 at 11:32