Does a Python equivalent to the Ruby ||= operator (“set the variable if the variable is not set”) exist?
Example in Ruby :
variable_not_set ||= 'bla bla'
variable_not_set == 'bla bla'
variable_set = 'pi pi'
variable_set ||= 'bla bla'
variable_set == 'pi pi'
No, the replacement is:
However, wanting to use this construct is a sign of overly complicated code flow. Usually, you’d do the following:
and never be unsure whether
vis set or not. If it’s one of many options that can either be set or not, use a dictionary and itsgetmethod which allows a default value.