3

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.

1

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

4 Comments

Don't you mean super(A, self).__init__() instead super(B, self).__init__()?
@scr: Nope. It takes the current class.
I know that removing underscores brings 2, but why does it return 1 with them??
If super(B, self).__init__() is omitted, b.get_value() still returns 2. Why the need for super()?
3

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.

2 Comments

Python has neither "private" nor "protected"; both are just conventions, one of which the compiler acts upon.
@Ignacio sure, that is why I enclosed these words in quotes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.