Fork me on GitHub
#other-languages
<
2022-11-18
>
Ngoc Khuat04:11:31

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

phronmophobic04:11:12

Wouldn't that just be a global variable?

phronmophobic04:11:46

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

Ngoc Khuat04:11:53

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

phronmophobic04:11:44

I thought python was still operated under the GIL

Ngoc Khuat05:11:05

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

phronmophobic05:11:28

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.szabo14:11:50

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

mauricio.szabo14:11:51

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