Skip to content

Comment on Don't let dicts spoil your code (2020)parent

Comments

Python implicit string iteration is an annoying trap.

  COMPLETE_STATES = 'done', 'cancelled'

  if state in COMPLETE_STATES:
      …
Then you decide to handle cancelled tasks separately.
  COMPLETE_STATES = 'done'
Boom!

Can't remember a less contrived example right away, but I have broken real code by inadvertently calling string iteration and spent some time scratching my head. Granted, don't think I've seen something like this in a PR, only in local development.

P.S. I think last time I stumbled on this, it actually involved Django ORM and changing filter(state__in=COMPLETE_STATES) to filter(state__in=DONE).

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.