Live data from Hacker News

A primer on Python decorators

thumbtack.com

41–45 of 45 posts

Re: A primer on Python decorators

#41
post #29
post #20

Earlier quoted context omitted.

The Python community generally advocates an "it's easier to ask for forgiveness than permission" coding style. When faced with a condition of the form "if condition a holds, do b, else c", it's very often a better idea to do "let's try b, and do c in case b fails because condition a didn't hold". 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…

Agree with that "try except" is better than "if else" for file handling ("with" is even better). I am not sure if "try except" is really faster than "if else" in some edge cases in a memoization context, as you claim. What I am sure is that in a didactic context, where you want people to understand code, something like: if args not in stored_results: stored_results[args] = fn(*args) return stored_results[args] is muc…

For a memoize function, try/except is perfect. You gain a slight speed benefit from function calls that are memoized as compared to the "if x not in y", and since memoize is all about speeding up repetitive function calls, it's the most optimal choice. Using the "if x not in y" you are giving up speed of the "x is actually in the y" in exchange for a cheaper than "except", however if you are memoizing a function, chances are that the function is going to eat up processing power making your optimization of the "exceptional case" at the expense of the "normal case" not worth it.

As far as people understanding code - this is a common pattern in python code, try / except should be easy for anyone to understand.

Re: A primer on Python decorators

#42
post #40
post #16

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.

The try/except is the preferred python way of doing things. In Python, try/except is cheap, and in the case where the try actually succeeds, you can gain a slight performance benefit. In this case, since performance is the goal, it is perfect for memoize.

I think subtle optimization should be left aside in a tutorial. Try except is a good way to handle many cases that are not exceptions, like when reaching the end of an iteration, but I never heard that it was "the preferred way" for checking the presence of a key in a dict. I believe the "if in" construct is cleaner, clearer and "pythoniker", if you don't mind the Housism.

Re: A primer on Python decorators

#43
post #37
post #16

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.

I'm no pythonist, but I think setdefault would be perfect for the case http://docs.python.org/library/stdtypes.html#dict.setdefault

I agree. The author uses exception for something that is not at all exceptional. In fact, it will occur at least once. This is misuse of an exception. No one expects Spanish Exception!

Earlier, he uses the map function for no apparent reason. I mean - list comprehension would perfectly fit here.

I still think I have something to learn from the article, though.

Re: A primer on Python decorators

#44
post #6

Earlier quoted context omitted.

Thanks! It is indeed the first in a series of Python posts that will be on the Thumbtack engineering blog.

Can you please email all future posts to me? ;p zackster@gmail.com -- I don't use RSS any more, and I'd hate to miss them.

Or just let me use Feedburner to subscribe via email.

Re: A primer on Python decorators

#45
post #42
post #40

Earlier quoted context omitted.

The try/except is the preferred python way of doing things. In Python, try/except is cheap, and in the case where the try actually succeeds, you can gain a slight performance benefit. In this case, since performance is the goal, it is perfect for memoize.

I think subtle optimization should be left aside in a tutorial. Try except is a good way to handle many cases that are not exceptions, like when reaching the end of an iteration, but I never heard that it was "the preferred way" for checking the presence of a key in a dict. I believe the "if in" construct is cleaner, clearer and "pythoniker", if you don't mind the Housism.

"if in" is not the preferred way to check for the presence of a key in a dict. You should use setdefault. The thing is, you aren't searching for a key in a dict only. You're memoizing. And try/except is the most efficient way to do that. This article isn't teaching you how to program, it's teaching decorators.
Post reply on HN