Refactor len(...) == 0
to not ...
#7996
brettcannon
started this conversation in
Ideas
Replies: 2 comments 2 replies
-
Am I correct that this could return a different result for a pathological class? E.g.: class Foo:
def __bool__(self):
return False
def __len__(self):
return 1
foo = Foo()
print(not foo)
print(len(foo) == 0) (I'm supportive of adding the rule and the fix -- just trying to understand when we can / can't guarantee that it's a safe transform.) |
Beta Was this translation helpful? Give feedback.
2 replies
-
I believe some people might use such an transformation, but I can imagine scenarios where it fails quickly. For example, create a function that works with both lists and numpy arrays (like in sklearn). You might be able to call |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
See https://snarky.ca/unravelling-not-in-python/ as to how this is equivalent to checking for the length of a container. It is also potentially faster as it avoids the container potentially having to calculate the length and creating the integer compared to returning a singleton.
Beta Was this translation helpful? Give feedback.
All reactions