On Python's @property Decorator

@property decorator is an excellent way to reduce the readability of Python code. It obfuscates a perfectly good function call and tricks readers into thinking they’re performing a regular attribute access or assignment. Unless there’s a really good and explicit reason to do this, don’t. List of Good and Explicit Reasons: Refactoring That’s pretty much it. If you need to turn something that (rightfully so) started out as a simple attribute, but with time accrued some more complex logic, @property is a good way to gracefully transition from attributes to function calls. ...

February 18, 2024

Why I Always Assign Intermediate Values to Local Variables Instead of Passing Them Directly to Function Calls

Instead of def do_something(a, b, c): return res_fn( fn(a, b), fn(b), c ) I do: def do_something(a, b, c): inter_1 = fn(a, b) inter_2 = fn(b) result = res_fn(inter_1, inter_2, c) return result The first version is much shorter, and when formatted properly, equally readable. But the reason I prefer the second approach is because all intermediate steps are saved to local variables. Exception tracking tools like Sentry, and even Django’s error page that pops up when DEBUG=True is set, capture the local context. On top of that, if you ever have to step through the function with a debugger, you can see the exact return value before stepping out from the function. This is the reason why I even save the final result in a local variable, just before returning it. ...

November 29, 2023