3 ms·
A better example would be something like: def my_calc(x, scale = None): if not scale: scale = 1 return (x + 4) * scale where the
by fiddlerwoaroof 7y ago
A better example would be something like:
def my_calc(x, scale = None):
if not scale:
scale = 1
return (x + 4) * scale
where the (x + 4) bit stands for some interesting calculation whose result is being scaled: this code makes it unclear whether or not scale == 0 is intentionally conflated with scale == None or whether this is a programming error. It would be better to do this, if this is intentional:
def my_calc(x, scale = None):
if scale == 0 or scale is None:
scale = 1
return (x + 4) * scale
Or, if it's unintentional, one should have written:
def my_calc(x, scale = None):
if scale is None:
scale = 1
return (x + 4) * scale
- taeric 7y agoWhy not just default scale to 1? I get that you might want to know if it was supplied or if it is the default, and common lisp has a way to do that. It is very niche to need, thought. That all said, I think this is just suffering the fate if anything with examples. I accept there are places where zero as false can be annoying.
- bb88 7y agoYes. Or set scale to 0 or something. Generally speaking we're creating contrived examples because in all fairness, I've only seen this once I can remember in the last 5 years of writing business logic in python. Usually language purists would prefer us to be explicit in our languages. And type safety is the best thing since sliced bread. But effectively that's why python has taken off. Types do matter, but for most things they don't Lots of good software has been written using python.