Why is getattr not working? I am not trying to set a default value to threadLocal, I want to know why getattr does not work the way I want it to? tried to lock, same output
Expected Output
0
1
2
3
4
main thread
Current Output
0
0
0
0
0
main thread
Code
from concurrent.futures import ThreadPoolExecutor
from threading import local
threadLocal = local()
threadLocal.x = 'main thread'
def f(x):
# threadLocal.x = x # this works
threadLocal.x = getattr(threadLocal, 'x', x) # this does not work
return threadLocal.x
pool = ThreadPoolExecutor(5)
result = pool.map(f, range(0, 5))
for i in result:
print(i)
print(threadLocal.x)
xfrom the main thread.threadLocal.x = xdoes what I want, why does not getting the value using getattr work?xingetattr(threadLocal, 'x', x)will not be a function local the one in the function argument? @KlausD.