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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…