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!

5
  • Since that is not valid, it's unclear what you want it to mean. Commented Feb 11, 2020 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. Commented Feb 11, 2020 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 stackoverflow.com/questions/394809/… second Post. is kind of out of the box thinking in my opinion. Commented Feb 11, 2020 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? Commented Feb 11, 2020 at 23:32
  • @HeapOverflow Sorry. Does the Edit clarify what I was thinking about? Commented Feb 11, 2020 at 23:43

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

5 Comments

In which Python version is this valid?
@user2853437 In 3.8.
Thank you. That's exactly what I was wondering. So it is possible but starting from version 3.8
So a kind of better approach would be rival, lival = (m,lival) if (fl*fm) < 0 else (rival,m) ?
@user2853437 Hmm... I'd say that's better in some way and worse in another. I'd just stick to the normal way that you have in your question.
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.

1 Comment

@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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.