开发者

Python, pythonic expression

开发者 https://www.devze.com 2023-02-09 18:13 出处:网络
Is there pythonic expression for thi开发者_如何学Gos snippet? # linux.cmd is a dict cmd = linux.cmd.get(k, k)

Is there pythonic expression for thi开发者_如何学Gos snippet?

# linux.cmd is a dict
cmd = linux.cmd.get(k, k)
if not cmd:
    cmd = k

The value of linux.cmd.get[k] could be False.


All you need is the first line, sice the second paramter to dict.get() is the default value anyway. That construct returns k if k is not in dict. IF dict can return a value that evaluates to false add a " or k" to the end of the first line.

0

精彩评论

暂无评论...
验证码 换一张
取 消