[英]Is there some kind of tuple wildcart in Python?
For instance, in Erlang one can use an usual pattern matching wildcart while reading tuples. 例如,在Erlang中,可以在读取元组时使用通常的模式匹配wildcart。 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? 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: 3.x让您不必一次全部关心那些:
r, *x = color()
Or indexing is always an option. 或索引总是一个选项。
r = color()[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.