Question

For instance, in Erlang one can use an usual pattern matching wildcart while reading tuples. Say, I want to read a red channel value from a color. Instead of writing:

{R, G, B, A} = color()

I could do:

{R, _, _, _} = color()

'_' here stands for everything I don't care about. This syntax makes code a little bit cleaner on unnecessary variables.

Is there something like this in Python?

Was it helpful?

Solution

Not per se, since _ is a valid variable name. It is possible to unpack the same way though:

r, x, x, x = color()

3.x lets you not care about the ones on the end all at once:

r, *x = color()

Or indexing is always an option.

r = color()[0]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top