Marco Sulla writes:
Maybe it's a crazy idea, but what if we could decorate a single line of code? For example:
@Timer a = time_consuming_function()
As syntax, I don't see the advantage over
with ContextManagingTimer(): a = time_consuming_function()
I don't understand the intended semantics. There's no there there to decorate. How do you propose to pick the sequence of bytecodes to decorate? What happens if the're not bytecodes and the machine code JIT optimizer (think common subexpression elimination) got hold of it before you decorated it?
This will be equivalent to using Steven's context manager, but the decorator is more simple to comment and uncomment.
To the extent that this abbreviated notation "does" something worth the needed mucking with decorator semantics, you're going to be doing it a lot, right? If so, why not tune your ContextManagingTimer to take a debug argument, store its results in a class attribute, etc, etc. Seems more straightforward to me.
Maybe it could be possible to comment also code blocks:
@parallel for x in y: do_something(x) now_something_completely_different(x)
If you can figure out what to decorate a simple statement, I don't see why you couldn't do it with a compound statement. However, Python has *suites* (loosely associated sequences of statements), not *blocks*. Why is that important? Well, suppose you want to identify the x in y that goes exponentially complex on you:
for x in y: @ContextManagingTimer # creates a stack of results in Timer.out do_something(x); now_something_completely_different(x)
but unfortunately we don't have blocks and semicolons don't work like that.
This could be a shortcut for multiprocessing.
I don't think there are any shortcuts for multiprocessing. ;-)