1

In JavaScript I can choose to be agnostic of the complete structure of some returned value during destructured assignment:

const returnsFour = () => [1, 2, 3, 4];

const [ a, b, ...c ] = returnsFour();

Now a is 1 and b is 2 and c contains whatever else was in that Array.

Can I do anything like this in Python 2.7?

A naive translation doesn't work, because the * operator only works to group stuff when in a function signature:

returnsFour = lambda: (1, 2, 3, 4)

a, b, *c = returnsFour()  # syntax error

The next thing I would try is just grabbing indices of the result, but this is less pretty when you want several different indices as variables.

results = returnsFour()
a = results[0]
b = results[1]
Chris
  • 6,805
  • 3
  • 35
  • 50

0 Answers0