2

A Python-function foo(p, q) calculates four values a, b, c, and returns

return a, b, c, d

In the calling function I need an assignment like

(r, s, t, u) = (p, q, foo(p, q))

or

((r, s), (t, u)) = ((p, q), foo(p, q))

How does the code look like?

2
  • Something like r, s, t, u, *_ = (p, q) + foo(p, q) would also work if you don't care about c and d that's being returned. Commented Jul 1, 2022 at 17:44
  • Optionally, you could use r, s, (*_, t, u) = (p, q, foo(p, q)) to discard a and b. Commented Jul 1, 2022 at 17:54

1 Answer 1

2

The structure of the receivers should be the same as what's being assigned and returned.

r, s, (w, x, y, z) = p, q, foo(p, q)

If you only want the last two elements of the foo(p, q) respnse you can slice it:

r, s, (w, x) = p, q, foo(p, q)[2:]

or use * in the assignment:

r, s, (*_, w, x) = p, q, foo(p, q)
Sign up to request clarification or add additional context in comments.

Comments

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.