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

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

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

Why does shadowed variable evaluate to undefined when defined in outside scope?

Variables are subject to hoisting. This means that regardless of where a variable is placed within a function, it is moved to the top of the scope in which it is defined. For example: var outside_scope = “outside scope”; function f1() { alert(outside_scope) ; var outside_scope = “inside scope”; } f1(); Gets interpreted into: var … 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

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