Skip to content

Possible code improvement suggestion: Use is not None rather than bool() for None check #32720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
skasturi opened this issue Mar 15, 2020 · 1 comment · Fixed by #32721
Closed
Labels
Code Style Code style, linting, code_checks
Milestone

Comments

@skasturi
Copy link
Contributor

skasturi commented Mar 15, 2020

Code Sample, a copy-pastable example if possible

While going through the code I observed at couple of places where bool(o) is being used to check whether the object o is None.

Example 1:

f = lambda x: bool(regex.search(x))

Example 2:
f = lambda x: bool(regex.match(x))

Problem description

bool(o) is several times slower than o is not None (see below). In the above cases however, majority of the time indeed is being taken by the re.search and therefore it might not matter for the overall time. But, why not switch to the possibly correct version?

Moreover, the functionality remains the same as re.search and re.match return None when they cannot find a match; which is fundamentally what is being checked in this code.

image

@skasturi
Copy link
Contributor Author

... and I will be sending out a PR to fix this.

@simonjayhawkins simonjayhawkins added the Code Style Code style, linting, code_checks label Mar 17, 2020
@simonjayhawkins simonjayhawkins added this to the 1.1 milestone Mar 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Style Code style, linting, code_checks
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants