str='test'
example={'test':'value',}
return str in example and example[str] or None
why the seemingly redundant extra test for key str in example?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In this specific example, the check is to first make sure that ‘test’ is actually a valid key in the example dict, otherwise you would get a KeyError exception. Then the logic proceeds to check the key and either return it, or a None if the value of example[str] evals to False
It would be a lot easier if this example simply did:
Update
Even simpler, since the extra param to get() is not needed:
Update 2: Breaking down the truth tests and boolean operations from the OP (based on comments)