Hacker News new | ask | show | jobs
by kivle 1458 days ago
I do agree that it's probably unnecessary here, but in many cases assigning to variables can be a good thing in the name of readability. You can give the variable a name, explaining it's purpose. If you directly return an expression, the reader will have to parse and understand the purpose of the expression to understand the context.
1 comments

As of 3.10, the walrus operator is your friend if you really want to name to explain:

    return cached_foobar := cache.get("foobar").data
Wouldn’t this throw an attribute error if cache.get didn’t return an object with .data?
Yes, just as it would if you made the assignment on a line previous to the return statement.

It would also error if there was no 'cache' in scope, and anything else you can imagine.