On Jul 15, 9:13 pm, Steven D'Aprano <steve +comp.lang.pyt...@pearwood.info> wrote:
> I have just written a bunch of code with about two dozen examples similar > to this: > > for item in (seq or []): > do_something_with(item) > > iterates over seq if it is non-empty, or the empty list. Writing it like > this would be more painful, more complex, less readable and less > idiomatic: > > if seq is not None: > for item in seq: > do_something_with(item) > > not to mention completely unnecessary if you have already checked that > seq is either None or a sequence, and not some other arbitrary value. Short circuitry is a powerful tool! But why the heck would your sequences ever be None? Are you using None as a default? And if so, why not use an empty sequence instead ([], {}, "")? -- http://mail.python.org/mailman/listinfo/python-list