> unlike in Java, you can also call a class method on an instance
It's possible in Java, too. It's just considered bad practice.
Still, a very nice read!
11–20 of 45 posts
> unlike in Java, you can also call a class method on an instance
It's possible in Java, too. It's just considered bad practice.
Still, a very nice read!
http://stackoverflow.com/questions/739654/understanding-pyth...
Love using Python, but still haven't got around to using decorators yet. I probably didn't understand the concept properly until now ...
Great primer, reading it gives a good sense for why the decorator syntax was made and the potential uses. Is this part of a wider series you are running?
Thanks! It is indeed the first in a series of Python posts that will be on the Thumbtack engineering blog.
Minor nitpicking: > unlike in Java, you can also call a class method on an instance It's possible in Java, too. It's just considered bad practice. Still, a very nice read!
Thanks, but my favorite explanation of decorators is still this stackoverflow answer (see the second answer) http://stackoverflow.com/questions/739654/understanding-pyth...
Thanks, but my favorite explanation of decorators is still this stackoverflow answer (see the second answer) http://stackoverflow.com/questions/739654/understanding-pyth...
To authors: I would avoid try except in this code snippet, a simple if else is more explicit. I would also avoid a = b = c statement. One line per statement is better most of the time.
In this case it's better because you can avoid computing an extra hash of the object in cases where it's already a key of the dictionary. This may seem like a silly optimization, but it can very easily add up if you're accessing existing elements most of the time--I once had a bit of code that went 60x faster when I replaced an if-else with a try-except.
In other cases it can be even more beneficial. Say you're opening a file. One approach to avoid errors would be to check if a file exists first. This is error-prone because the file might cease to exist in between the 'if' and the 'open' statements, and now you have no code written to handle the error. Using try-except will ensure that you actually handle the error intelligently.
This isn't to say there's never a good reason to use an 'if' to check things, just that if you can do it in one step instead of two, one is usually better.