I can’t seem to find a python way of saying
x = y or 1
In Perl:
$x = $y || 1;
Is there a nice way to do this 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.
Python’s
orhas what I believe are the semantics you’re looking for. See the documentation. It’s important to realize that it returns the input value(s), notTrueorFalse.This should mean that
x = y or 1should be what you’re after,xwill beyifyis non-False, else it will be1.