Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python oop polymorphism

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 = 2

b = B()

print b.get_value()

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

like image 934
alexvassel Avatar asked Dec 30 '25 02:12

alexvassel


2 Answers

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
like image 108
Nick Bastin Avatar answered Dec 31 '25 15:12

Nick Bastin


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.

like image 29
Roman Bodnarchuk Avatar answered Dec 31 '25 14:12

Roman Bodnarchuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!