other-languages 2022-11-18

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

Wouldn't that just be a global variable?

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

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

I thought python was still operated under the GIL

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

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

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

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