Ruby supports this:
name = name || "default"
If I try it in python:
name = name or "default"
Interpreter reports:
NameError: name 'name' is not defined
What is the equivalent of the short circuit evaluation assignment in python?
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.
or, more succinctly:
Substitute
locals()inside functions.Possibly it would be better to just
try/except:As a side note, I would never use either of these idioms in my code. (Of course, I wouldn’t use the other idioms from Javascript or Ruby that you mentioned). I’d make sure my variables were declared to the default values at the outset and then change them to non-default values as the need arises.