Skip to content
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

Replace try/except with hasattr #117

Closed
AndreasArvidsson opened this issue Dec 5, 2021 · 2 comments
Closed

Replace try/except with hasattr #117

AndreasArvidsson opened this issue Dec 5, 2021 · 2 comments
Labels
code quality Improvements to code quality

Comments

@AndreasArvidsson
Copy link
Member

Today we used the following pattern:

try:
    m.cursorless_modifier
except AttributeError:
    m.cursorless_another_modifier

We might want to change into:

if hasattr(m, "cursorless_modifier"):
    m.cursorless_modifier
else:
    m.cursorless_another_modifier

Am not convinced of which pattern is best at this time. I just wanted to raise the question.

@AndreasArvidsson AndreasArvidsson changed the title Replace try/except wit hasattr Replace try/except with hasattr Dec 5, 2021
@AndreasArvidsson AndreasArvidsson added the code quality Improvements to code quality label Dec 5, 2021
@pokey
Copy link
Member

pokey commented Dec 5, 2021

I've been using the try / except style as EAFP is generally more pythonic. But see here for a more nuanced discussion. Could see a case made for either direction. Kinda depends what we're trying to convey with the control flow, eg what is "expected" vs "exceptional"

@AndreasArvidsson
Copy link
Member Author

So basically the intent is that is it a normal use case or the exception that the key is missing. In many of our cases its a or statement between the two different keys where both can be as equally likely. I think that the try except pattern we have works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality Improvements to code quality
Projects
None yet
Development

No branches or pull requests

2 participants