2

Is there pythonic expression for this 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.

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

the value of linux.cmd.get[k] could be false
Oh, then you can add " or k" to he end of the first line. Better would be ensure there was no false value possible.
ok thanks, that works, can you mod your answer likewise too? I need false values so that k is value as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.