4 ms·
This is a bit of bikeshedding, but I think if not n in memo: is more naturally written as if n not in memo:
by robertlagrant 3y 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:
- progmetaldev 3y agoAs 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.
- alfalfasprout 3y agoI 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.
- kstrauser 3y agoI 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.
- stavros 3y agoLinters agree with you with the default config, and will warn on "if not x in y".
- zbentley 3y agoAgree. 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).