[英]Python equivalent to picking first non-null argument
In JavaScript I can do 我可以用JavaScript做
const foo = bar || baz || bloop || blop || bleep || null
If I want foo
to be equal to the first one of the above arguments that is not falsey / null 如果我想让
foo
等于上述不是falsey / null的第一个参数
Is there an idiomatic Python equivalent? 是否有惯用的Python等效项? I was just going to loop through a list of args, but thought I'd check.
我只是要遍历args列表,但以为我会检查一下。
@zvone is correct. @zvone是正确的。 For instance:
例如:
banana = True
tom = 4
hotdog = None
goo = hotdog or tom or banana
shazam = banana or hotdog or tom
In this example, goo
is 4
, and shazam
is True
. 在此示例中,
goo
为4
, shazam
为True
。 So you can mix types! 这样您就可以混合类型! Cool stuff.
很酷的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.