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

Interesting Use of Try/Except Statement #1

Closed
shohtime opened this issue Jan 10, 2019 · 1 comment
Closed

Interesting Use of Try/Except Statement #1

shohtime opened this issue Jan 10, 2019 · 1 comment

Comments

@shohtime
Copy link

" try:\n",
" frequency_table[word] += 1\n",
" except:\n",
" frequency_table[word] = 1\n",

This is an interesting use of a try/except statement to access a dictionary's values. Although I must admit that it's clever, I can't say I'm a fan. Beyond the scope of this course, you learn that a try/except statement is relatively costly in terms of efficiency compared to a simple if statement, especially when an exception is actually raised (as it is in this case many times). You should save such a statement for when it is necessary, and instead use built-in functions for dictionaries, such as .get() here.

@jwkeepers
Copy link
Owner

Thank you for your comments, so noted.

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

No branches or pull requests

2 participants