Hacker News new | ask | show | jobs
by robertlagrant 1057 days ago
This is a bit of bikeshedding, but I think

  if not n in memo:
is more naturally written as

  if n not in memo:
5 comments

I agree. The first is order of operations dependent. Without looking, is that `(not n) in memo` or `not (n in memo)`?

The sent can only be interpreted one way.

As someone learning Python, but having worked with other languages, I think your second example is better as it reads more like English. I think that simplicity actually ends up much more rewarding when it comes to reading code.
Linters agree with you with the default config, and will warn on "if not x in y".
Agree. Using "not in" can also theoretically make certain checks faster (e.g. testing negative presence in a hash-based data structure can bail out without walking the collision chain if the initial hashed location does not have an element).
I would absolutely point this out in a code review. It's not even that pedantic, it's the kind of code that causes a double take b/c.