سؤال

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