Reassigning a name to yourself

Pay attention to these lines in the package of standard multiprocessing libraries:

 dict = dict list = list 

What is the point of reordering some of the names already available on __builtins__ in the module area? What is he trying to achieve? I was looking for an explanation in git wine, but this commit was great and there was no relevant comment.

+49
python
May 08 '17 at 17:05
source share
1 answer

This code is found in multiprocessing.dummy , a "fake" version of multiprocessing that implements functionality with threads. If you look down a few lines, you will see

 def Manager(): return sys.modules[__name__] 

multiprocessing.dummy implements Manager as a function that simply returns the multiprocessing.dummy module itself, so the multiprocessing.dummy module object must provide the API of the multiprocessor Manager object. Lines

 dict = dict list = list 

so you can do

 m = multiprocessing.dummy.Manager() d = m.dict() 

as if you had real multiprocessing.Manager() .

+47
May 08 '17 at 17:16
source share
— -



All Articles