other-languages

Ngoc Khuat 2022-11-18T04:33:31.070779Z

Do we have anything in Python that is equivalent to Clojure’s dynamic binding?

phronmophobic 2022-11-18T04:42:12.645809Z

Wouldn't that just be a global variable?

phronmophobic 2022-11-18T04:43:46.240209Z

you could use that with a context manager to set and unset a global variable for a similar effect

Ngoc Khuat 2022-11-18T04:48:53.710929Z

I don’t think global variables do the job. it’s not thread-safe right?

phronmophobic 2022-11-18T04:49:44.876809Z

I thought python was still operated under the GIL

Ngoc Khuat 2022-11-18T05:00:05.857749Z

I don’t know much about GIL but I tried it and seems like it doesn’t

Ngoc Khuat 2022-11-18T05:00:42.731749Z

bigger

phronmophobic 2022-11-18T05:45:28.782399Z

oh yea. you're right. I guess you'd have to use a thread local, https://docs.python.org/3/library/threading.html#thread-local-data

mauricio.szabo 2022-11-18T14:43:50.130349Z

An interesting things is that binding is also not thread-safe on ClojureScript 😢

mauricio.szabo 2022-11-18T14:55:51.096539Z

An example code. If it was thread safe, it would print:

:before :first :first
:before :second :second
:after :first :first
:after :second :second
And keep the value of the var as :none