Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-assigning a name to itself

Tags:

python

Notice these lines in multiprocessing package of standard libraries:

dict = dict list = list 

What's the point of rebinding some names already available on __builtins__ into the module scope? What is it trying to achieve? I searched for an explanation in the git blame, but this commit was large and there was no relevant comment.

like image 346
wim Avatar asked May 08 '17 17:05

wim


People also ask

Can we replace self with some other name?

self is only a reference to the current instance within the method. You can't change your instance by setting self .

How do you reassign a variable?

In JavaScript, we can simply reassign the new value to the variable. When we change the value of a variable, we do not use var again. We simply use the = to give the variable a new value.

What does it mean to reassign a variable in JavaScript?

Variables in JavaScript do not have any type attached. Once you assign a specific literal type to a variable, you can later reassign the variable to host any other type, without type errors or any issue. This is why JavaScript is sometimes referred to as “untyped”. A variable must be declared before you can use it.

Can a variable have the same name as a function Python?

Bottom line: you can't have two things simultaneously with the same name, be it a function, an integer, or any other object in Python. Just use a different name.


1 Answers

This code occurs in multiprocessing.dummy, a "fake" version of multiprocessing that implements the functionality with threads. If you look down a few lines, you'll see

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

multiprocessing.dummy implements Manager as a function that just returns the multiprocessing.dummy module itself, so the multiprocessing.dummy module object has to provide the API of a multiprocessing Manager object. The lines

dict = dict list = list 

copy the bindings for the dict and list names from the builtins namespace into the module's namespace, so you can do

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

as if you had a real multiprocessing.Manager().

like image 89
user2357112 supports Monica Avatar answered Oct 09 '22 08:10

user2357112 supports Monica