开发者

python oop polymorphism

开发者 https://www.devze.com 2023-03-13 06:46 出处:网络
I can\'t understand why this code: class A(object): def __init__(self): self.__value = 1 def get_value(self):

I can't understand why this code:

class A(object):
    def __init__(self):
        self.__value = 1
    def get_value(self):
        return self.__value

class B(A):
    def __init__(self):
        A.__init__( self )
        self.__value =开发者_StackOverflow社区 2

b = B()

print b.get_value()

gives 1, but not 2. Thanks for your help.


Your problem is that double underscores are special in python, and create some modicum of privacy (not enforced, but it mangles the names, which is what is affecting you here). You should recreate this without the variable being named with double underscores. Also, you should use super instead of calling A.__init__ explicitly:

>>> class A(object):
...     def __init__(self):
...             self.value = 1
...     def get_value(self):
...             return self.value
... 
>>> class B(A):
...     def __init__(self):
...             super(B, self).__init__()
...             self.value = 2
... 
>>> b = B()
>>> b.get_value()
2

For more specifics if you don't want to read the referenced documentation:

If you read the link on "special" above, that paragraph describes the internal name mangling that happens when you use __. The short answer is that A.get_value() returns _A__value, and setting self.__value in B actually sets a member variable named _B__value, which means that A.get_value() never sees it.

You can prove this to yourself by indeed doing something truly hinky:

>>> class A(object):
...     def get_value(self):
...             return self._B__value
... 
>>> class B(A):
...     def __init__(self):
...             self.__value = 2
... 
>>> b = B()
>>> b.get_value()
2


self.__value = 1 creates 'private' field, which is invisible for children in a such way.

Use single underscore for 'protected' fields.

E.g. self._value = 1

When you creates field with name started with double underscore, Python makes some modification on it before adding to object's __dict__:

>>> class A(object):
...     def __init__(self):
...         self.__value = 1
...     def get_value(self):
...         return self.__value
...
>>> A().__dict__
{'_A__value': 1}

That is why __value field is not visible in child object.

0

精彩评论

暂无评论...
验证码 换一张
取 消