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?

有帮助吗?

解决方案

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]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top