In python:
>>> a = b or {}
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.
I don’t think it has an official name, it’s just a clever/lazy way to be concise. It’s roughly equivalent to:
or:
I wrote this as a comment but I think it’s worth mentioning here:
You have to be very careful when using this trick. If your intention is to set a default value when something is None, it would be better to write that
is Nonetest explicitly than use this trick. The reason is that None is not the only thing that evaluates to false in a boolean context. 0, [], {}, … also evaluate to false. So this trick might not give you the expected results if you didn’t think about these special cases.Here’s one way to do it that is safer: