logoalt Hacker News

dijksterhuis04/21/20252 repliesview on HN

the thing that bothered me most reading through it was using decorators to mutate some global state with the `data` list variable.

like… it… just… it felt wrong reading that in the examples. felt very `def func(kw=[])` adjacent. i can see some rare uses for it, but eh. i dunno.

(also didn’t find the closure stuff that insightful, ended up skipping past that, but then i know decorators, so… maybe useful for someone else. i dunno.).


Replies

t-writescode04/21/2025

My "proudest" use of decorators has been in adding metrics gathering for functions in python, so you'd get automated statsd (and later prometheus) metrics just by having @tracked (or whatever I had its name be - it's been like 7 years) on the function header.

In a sense, that was mutating a global variable by including and tracking the metrics gathering. I imagine this person's early professional exposures to it and need to create their own also came from a similar situation, so "mutating global state" and closures sorta clicked for them.

People learn things by coming to those things from many different entry points and for many different reasons. This is another one of those instances :)

maleldil04/23/2025

> it was using decorators to mutate some global state

It isn't. The original version was doing that, but the "decorator" one wasn't. The data variable is internal to the closure, so different invocations of the decorator would have different data variables.

> didn’t find the closure stuff that insightful

It's used to attach state to a function. Different invocations of the function would have different state. IME, I'd rather use an explicit class for that, but it's useful with decorators.