简体   繁体   English

Python等效于选择第一个非null参数

[英]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 . 在此示例中, goo4shazamTrue So you can mix types! 这样您就可以混合类型! Cool stuff. 很酷的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM