如何清除Python threading.local对象?
如何清除Python实例的所有属性?您可以清除它的底层属性如何清除Python threading.local对象?,python,multithreading,Python,Multithreading,如何清除Python实例的所有属性?您可以清除它的底层属性\uuuu dict\uuu: >>> l = threading.local() >>> l <thread._local object at 0x7fe8d5af5fb0> >>> l.ok = "yes" >>> l.__dict__ {'ok': 'yes'} >>> l.__dict__.clear() >>>
\uuuu dict\uuu
:
>>> l = threading.local()
>>> l
<thread._local object at 0x7fe8d5af5fb0>
>>> l.ok = "yes"
>>> l.__dict__
{'ok': 'yes'}
>>> l.__dict__.clear()
>>> l.__dict__
{}
>>> l.ok
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'thread._local' object has no attribute 'ok'
您还可以访问本地对象的字典:
>>> mydata.__dict__
{'number': 42}
>>> mydata.__dict__.setdefault('widgets', [])
[]
>>> mydata.widgets
[]
>>> mydata.__dict__
{'number': 42}
>>> mydata.__dict__.setdefault('widgets', [])
[]
>>> mydata.widgets
[]