What is variable shadowing?

后端 未结 3 626
夕颜
夕颜 2021-01-15 21:23

https://stackoverflow.com/a/37657923/8061009 states that for the program:

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

    de         


        
相关标签:
3条回答
  • 2021-01-15 21:31

    In Java, methods and fields are fundamentally different things, operating by entirely different rules. Only methods are inherited by subclasses; fields are specific to the class that declared them. If a subclass declares a field with the same name as one in a parent class, they are entirely unrelated; methods of the parent class continue to access the parent's version, and methods of the child class access its version. This is what is referred to as shadowing. If the parent class actually wanted to make its field available to children, it would have to define getter/setter methods for it.

    In Python, there is no such distinction - methods are basically fields whose value happens to be a function. Furthermore, all of the fields from the entire inheritance hierarchy are stored in a single namespace (typically implemented as a dict attribute named __dict__). If the child and parent use the same name for something, they are necessarily referring to the same object.

    0 讨论(0)
  • 2021-01-15 21:38

    Variable 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. Then the variable in the scope that you are in shadows (hides/masks) the variable in the outer scope.

    In the above code the variable i is being initialized in both the super class and the child class. So the initialization in the super class will be shadowed by the initialization in the child and class.

    m = Child() #we initialized the child class with i=7
    print(m.i) #eventhough we are calling a method in the super class the value of i in the super class is shadowed by the value we initialized the instance of the child class (m)
    m.doStuff() #same thing here
    
    0 讨论(0)
  • 2021-01-15 21:50

    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.

    0 讨论(0)
提交回复
热议问题