Questions tagged [shadowing]

In computer programming, shadowing occurs when a variable declared within a certain scope (decision block, method or inner class) has the same name as a variable declared in an outer scope. This can lead to confusion, as it may be unclear which variable subsequent uses of the shadowed variable name refer to, which depends on the name resolution rules of the language.

One of the first languages to introduce variable shadowing was ALGOL, which first introduced blocks to establish scopes. It was also permitted by many of the derivative programming languages including C++ and Java.

The C# language breaks this tradition, allowing variable shadowing between an inner and an outer class, and between a method and its containing class, but not between an if-block and its containing method, or between case statements in a switch block.

237 questions
328
votes
11 answers

What is the problem with shadowing names defined in outer scopes?

I just switched to PyCharm and I am very happy about all the warnings and hints it provides me to improve my code. Except for this one which I don't understand: This inspection detects shadowing names defined in outer scopes. I know it is bad…
Framester
  • 33,341
  • 51
  • 130
  • 192
288
votes
14 answers

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

When I try this code: a, b, c = (1, 2, 3) def test(): print(a) print(b) print(c) c += 1 test() I get an error from the print(c) line that says: UnboundLocalError: local variable 'c' referenced before assignment in newer versions…
tba
  • 6,229
  • 8
  • 43
  • 63
126
votes
2 answers

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

auto foo = "You're using g++!"; auto compiler_detector = [foo](auto foo) { std::puts(foo); }; compiler_detector("You're using clang++!"); clang++ 3.6.0 and newer print out "You're using clang++!" and warn about the capture foo being unused. g++…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
107
votes
14 answers

Why does "example = list(...)" result in "TypeError: 'list' object is not callable"?

I tried to use this code from a tutorial at the REPL: example = list('easyhoss') The tutorial says that example should become equal to a list ['e', 'a', 's', 'y', 'h', 'o', 's', 's']. But I got an error instead: >>> example =…
khan shah
  • 1,171
  • 2
  • 8
  • 11
103
votes
20 answers

Why does code like `str = str(...)` cause a TypeError, but only the second time?

I have some code like: def example(parameter): global str str = str(parameter) print(str) example(1) example(2) The first call to example works, but then the second time around I get an error like: Traceback (most recent call last): …
P'sao
  • 2,946
  • 11
  • 39
  • 48
91
votes
3 answers

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

I have a script named requests.py that needs to use the third-party requests package. The script either can't import the package, or can't access its functionality. Why isn't this working, and how do I fix it? Trying a plain import and then using…
idjaw
  • 25,487
  • 7
  • 64
  • 83
75
votes
5 answers

Why are my dplyr group_by & summarize not working properly? (name-collision with plyr)

I have a data frame that looks like this: #df ID DRUG FED AUC0t Tmax Cmax 1 1 0 100 5 20 2 1 1 200 6 25 3 0 1 NA 2 30 4 0 0 150 6 65 Ans so on. I want to summarize some…
Amer
  • 2,131
  • 3
  • 23
  • 38
44
votes
6 answers

Is this an example of variable shadowing in JavaScript?

I learnt about the term variable shadowing in Eloquent Javascript (Chapter 3), but I am trying to understand a precise, basic example of the concept. Is this an example of shadowing? var currencySymbol = "$"; function showMoney(amount) { var…
fakeguybrushthreepwood
  • 2,983
  • 7
  • 37
  • 53
35
votes
11 answers

What is Shadowing?

In C# what does the term shadowing mean? I have read this link but didn't fully understand it.
james
27
votes
6 answers

Global variable is logged as undefined when passed as parameter to setTimeout callback function

I have some JS code as below: var x = self.someAJAXResponseJSON; // x has some object value here. setTimeout(function(x){ console.log("In setTimeout:", x); // But x is undefined here }, 1000); So I want to pass x to the setTimeout callback…
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
26
votes
5 answers

What is variable shadowing used for in a Java class?

I'm reading my Deitel, Java How to Program book and came across the term shadowing. If shadowing is allowed, what situation or what purpose is there for it in a Java class? Example: public class Foo { int x = 5; public void useField() { …
Jayson
  • 2,021
  • 3
  • 25
  • 26
22
votes
4 answers

Inheritance at package visibility in Java

I am looking for an explanation for the following behavior: I have 6 classes, {a.A,b.B,c.C,a.D,b.E,c.F}, each having a package visible m() method that writes out the class name. I have an a.Main class with a main method that does some testing of…
TFuto
  • 1,361
  • 15
  • 33
21
votes
5 answers

Why does this program produce odd output when the variable is uninitialized?

int main() { int j = 0; int i = 0; for (j = 0; j < 5; j++) { printf("Iteration %d : %d ", j + 1, i); int i; printf("%d", i); i = 5; printf("\n"); } } The above code generates the following…
Milan
  • 403
  • 2
  • 8
19
votes
2 answers

Java 8 Double curly bracket initialization and name collision

The following class has an inner class called Entry. This code will not compile in Java 8 as the compiler assumes the Entry referenced within the double curly brace initializer is of type Map.Entry and not Scope.Entry. This code compiles in previous…
Briggs
  • 639
  • 5
  • 13
18
votes
1 answer

Remove Warning of Variable Shadowing in Kotlin

I couldn't find this info anywhere else. Variable shadowing is a great feature in my opinion, yet in Kotlin we get warned for it every single time, thus requiring us to use @Suppress("NAME_SHADOWING") in every instance of it, if we wouldn't like it…
Roger Oba
  • 1,292
  • 14
  • 28
1
2 3
15 16