Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is variable shadowing?

This Stack Overflow answer states that for the program:

class Parent(object):
    i = 5;
    def __init__(self):
        self.i = 5

    def doStuff(self):
        print(self.i)

class Child(Parent, object):

    def __init__(self):
        super(Child, self).__init__()
        self.i = 7

class Main():

    def main(self):
        m = Child()
        print(m.i) #print 7
        m.doStuff() #print 7

m = Main()
m.main()

Output will be:

$ python Main.py 
7
7

That answer then compares it to a similar program in Java:

The reason is because Java's int i declaration in Child class makes the i become class scope variable, while no such variable shadowing in Python subclassing. If you remove int i in Child class of Java, it will print 7 and 7 too.

What does variable shadowing mean in this case?

like image 322
user8174870 Avatar asked Dec 12 '18 00:12

user8174870


People also ask

What is variable shadowing in Java?

Shadowing in Java is the practice of using variables in overlapping scopes with the same name where the variable in low-level scope overrides the variable of high-level scope. Here the variable at high-level scope is shadowed by the low-level scope variable.

What is variable shadowing in Python?

Variable shadowing occurs when a variable defined in the inner scope has the same name as a variable in the outer scope. Consider the example below, here the variable inside the function has the same name as a global variable.

What is variable hiding and shadowing?

Variable Hiding happens when a variable declared in the child class has the same name as the variable declared in the parent class. In contrast, variable shadowing happens when a variable in the inner scope has the same name as the variable in the outer scope.

Is variable shadowing bad?

Variable shadowing is not an error syntactically. It is valid and well defined. However, if your intention was to use the variable from the outer scope, then you could consider it a logical error.


1 Answers

What does variable shadowing mean in this case?

Variable shadowing means the same thing in all cases, independent of context. It's defined as when a variable "hides" another variable with the same name. So, when variable shadowing occurs, there are two or more variables with the same name, and their definitions are dependent on their scope (meaning their values may be different depending upon scope). Quick example:

In [11]: def shadowing():
    ...:     x = 1
    ...:     def inner():
    ...:         x = 2
    ...:         print(x)
    ...:     inner()
    ...:     print(x)
    ...:

In [12]: shadowing()
2
1

Note that we call inner() first, which assigns x to be 2, and prints 2 as such. But this does not modify the x at the outer scope (i.e. the first x), since the x in inner is shadowing the first x. So, after we call inner(), and the call returns, now the first x is back in scope, and so the last print outputs 1.

In this particular example, the original author you've quoted is saying that shadowing is not occurring (and to be clear: not occurring at the instance level). You'll note that i in the parent takes on the same value as i in the child. If shadowing occurred, they would have different values, like in the example above (i.e. the parent would have a copy of a variable i and the child would have a different copy of a variable also named i). However, they do not. i is 7 in both the parent and child. The original author is noting that Python's inheritance mechanism is different than Java's in this respect.

like image 173
Matt Messersmith Avatar answered Sep 28 '22 06:09

Matt Messersmith