August 2019
Beginner
482 pages
12h 56m
English
In some cases, you just want to assign (or reassign) variables. Bad practice would involve doing the following:
name = name or 'Sigizmund'
Here, name will have a value of Sigizmund if it was equal to Sigizmund before, or is untruthy (equal to None, False, or zero). This is fine in certain cases, but can lead to uncertainty due to ambiguity. A better solution would be to use if instead:
name = name if name is not None else 'Sigizmund'
Here, the logic is exactly the same—name will have a value Sigizmund if not None, but stated more explicitly. One key difference is that the preceding statement will not consider untruthy values as None—if name is equal to False or zero, it will not be overwritten. Perhaps counterintuitively, ...