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 inChild
class makes thei
become class scope variable, while no such variable shadowing in Python subclassing. If you removeint i
inChild
class of Java, it will print 7 and 7 too.
What does variable shadowing mean in this case?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With