Python is easy, just use the contextmanager decorator to turn the generator into a context manager that is also at the same time a decorator factory! For real though i love this❤
This is cool, this decorator-context manager equivalence reflects that a decorator what basically is doing is "managing" the "context" of the wrapped function and that a context manager is basically is "decorating" an annonymous IIFE. One really beautiful abstraction.
Excuse me, what the hell. This is super useful! Plus, much easier to test! Holy crap, I will try to remember to use this. Was this a planned feature? Or a coincidence by how Python is built? Or added afterwards...?
Cool, didn't know you could do this. This can only be done when the decorator returns the unmodified output of the given function being called with unmodified input... correct? Clearly these kinds of decorators exist... like ones that register stuff, log stuff, etc... but a lot of decorators are used to modify either input, output, coerce stuff, etc. If this can't help in those cases the beginning of this video is actually a great reference for how to properly type regular decorators.
I don't even write python that much, randomly saw this vid in recommended. It looks amazing! Even without the fact that you don't need to type anything, it's so much nicer than defining 3 nested functions. Tbh python decorators are the only feature i miss in other languages, but they're so nice.
That's such a nice and smart simplification, completely decoupling the function typing rather than going generic with it. Can the context manager decorator do something like logging the first parameter while also timing it, or are the parameters/return values completely out of reach?
How long has it been like that? Holy - soooo much easier. Is there any downside to this? Why would you ever use the "old" way? Imagine not using functools either...
Damn wish I knew this sooner. However the drawback of not accessing arguments and return values kinda limit the scope of this approach (e.g. custom caching decorator)
Wait how you made nano like this ?? I would like to look at your dotfiles or something like this as I can't believe that nano can be like this. It's like vim or neovim but it's nicer.
2:40 is there a specific reason why you use time.monotonic instead of time.perf_counter, because as far as i can remember you have always used time.monotonic in your videos?
this sort of intentionally prevents you from doing nasty signature mutation. the concatenate example in the other video was to demonstrate the feature and not really something I would ever do in reality
Generator has a yield type (the type in the yield), send type (usually only required for coroutines) and return type (the final returned value after the generator is exhausted)
This is awesome, and makes for a much nicer typing experience. Quick question on the @timing_ctx example: is "try: finally:" really needed in this case, or just a "nice to have" for the sake of completeness? i.e. would the section after the "yield" be skipped on an exception inside the decorated function if we ditch the try/finally? I'm asking this because imho, the decorator "setup/teardown" sections would look even better/simpler/more readable without the extra indentation that the finally: block unfortunately imposes on the "teardown" section of it. Thanks in advance, asottile, you are a legend!