I'm surprised no one offered this answer. It's not as "built-in" as Ruby's ||= but it's basically equivalent and still a one-liner:
foo = foo if 'foo' in locals() else 'default'
Of course, locals() is just a dictionary, so you can do:
foo = locals().get('foo', 'default')
Answer from Keith Devens on Stack OverflowI'm surprised no one offered this answer. It's not as "built-in" as Ruby's ||= but it's basically equivalent and still a one-liner:
foo = foo if 'foo' in locals() else 'default'
Of course, locals() is just a dictionary, so you can do:
foo = locals().get('foo', 'default')
I would use
x = 'default' if not x else x
Much shorter than all of your alternatives suggested here, and straight to the point. Read, "set x to 'default' if x is not set otherwise keep it as x." If you need None, 0, False, or "" to be valid values however, you will need to change this behavior, for instance:
valid_vals = ("", 0, False) # We want None to be the only un-set value
x = 'default' if not x and x not in valid_vals else x
This sort of thing is also just begging to be turned into a function you can use everywhere easily:
setval_if = lambda val: 'default' if not val and val not in valid_vals else val
at which point, you can use it as:
>>> x = None # To set it to something not valid
>>> x = setval_if(x) # Using our special function is short and sweet now!
>>> print x # Let's check to make sure our None valued variable actually got set
'default'
Finally, if you are really missing your Ruby infix notation, you could overload ||=| (or something similar) by following this guy's hack: http://code.activestate.com/recipes/384122-infix-operators/
Python Conditional Variable Setting - Stack Overflow
A question about conditional variable assignment.
Best way to do conditional assignment in python - Stack Overflow
Python conditional variable assignment - Stack Overflow
I don't know if that's the actual name for it, but I just recently learned that I can do
variable = foo if condition == True else bar
Does it work with the += reassignment as well? Hypothetically, say I needed to do
if condition:
variable += 1
else:
variable -= 1
Would variable += 1 if condition else variable - 1 work?
The or operator does what you want:
get_something() or y
In fact, it's chainable, like COALESCE (and unlike ISNULL). The following expression evaluates to the left-most argument that converts to True.
A or B or C
You may use:
a = get_something() or y
If get_something is True in boolean context, its value will be assigned to a. Otherwise - y will be assigned to a.
Hello,
I'm still new to Python, and I'm trying to automate a task at work. My question is regarding attributes in classes.
In this class, the attribute will always have two values depending on the what number is given. I know if statements can be used inside the class, but no matter how I try to structure the code (moving around indentions, making the if statement into its own function inside the class), Python doesn't think the attribute exists. Can I define the attribute this way (making a decision inside the class), or do I have to pass in the 'model_name' property when I pass in 'number'? Thanks for helping a new guy.
class Generator:
def __init__(self, number):
self.number = number
model_name = 'gen1'
if number == 2:
model_name = 'gen2'
if __name__ == '__main__':
gen = Generator(2)
print(gen.number)
print(gen.model_name)Error I receive:
line 16, in <module>
print(gen.model_name)
AttributeError: 'Generator' object has no attribute 'model_name'