Importing a library from (or near) a script with the same name raises “AttributeError: module has no attribute” or an ImportError or NameError

This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name. An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name … Read more

Why does “example = list(…)” result in “TypeError: ‘list’ object is not callable”? [duplicate]

Seems like you’ve shadowed the builtin name list, which points at a class, by the same name pointing at an instance of it. Here is an example: >>> example = list(‘easyhoss’) # here `list` refers to the builtin class >>> list = list(‘abc’) # we create a variable `list` referencing an instance of `list` >>> … Read more

UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)

Python treats variables in functions differently depending on whether you assign values to them from inside or outside the function. If a variable is assigned within a function, it is treated by default as a local variable. Therefore, when you uncomment the line, you are trying to reference the local variable c before any value … Read more

Lambda capture and parameter with same name – who shadows the other? (clang vs gcc)

Update: as promised by the Core chair in the bottom quote, the code is now ill-formed: If an identifier in a simple-capture appears as the declarator-id of a parameter of the lambda-declarator‘s parameter-declaration-clause, the program is ill-formed. There were a few issues concerning name lookup in lambdas a while ago. They were resolved by N2927: … Read more

Shadowing and Nested function

Your code is equivalent to the following, where I’ve simply numbered instances of your names to help you visualize how shadowing is occurring. let func y0 = let dup0 y1 = y1 + y1 let z0 = dup0 y0 let dup1 y2 = let dup2 z1 = let y3 = y2 * z1 y3 let … Read more

Why is it not possible to shadow a local variable in a loop?

You can make a local variable shadow an instance/static variable – but you can’t make one local variable (your loop counter) shadow another local variable or parameter (your parameter). From the Java Language Specification, section 14.4.3: If a name declared as a local variable is already declared as a field name, then that outer declaration … Read more

Importing installed package from script with the same name raises “AttributeError: module has no attribute” or “ImportError: cannot import name”

This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name. An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name … Read more

What is Shadowing?

Shadowing hides a method in a base class. Using the example in the question you linked: class A { public int Foo(){ return 5;} public virtual int Bar(){return 5;} } class B : A { public new int Foo() { return 1;} public override int Bar() {return 1;} } Class B overrides the virtual method … Read more