Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
None, False, zero of any numeric type, and empty containers such as '', [], {} and set() are falsy; checking if value: can wrongly treat a valid 0 or empty string as missing.
Python calls bool, or len if bool is absent, to decide truthiness, so empty collections and zero values are falsy and almost everything else is truthy. if items: is idiomatic for checking that a list is not empty. The pitfall is using the same check for optional values: if price: skips a legitimate price of 0, and value or default replaces 0 or '' with the default. When the real question is whether a value was provided, use if value is None. Custom classes can define bool to control their own truthiness.