0

I there a way to directly access dundle method in sub-class of Python ?

Imagine I have the following classes :

class Parent(object):
    def __init__(self, x):
        self.x = x
    
    def __add(self,y):
        return self.x + y
    
    def addition(self,y):
        return self.__add(y)

class Children(Parent):
    
    def add(self,y):
        return self.__add(y)

I can obviously instantiate the two classes, and use addition on both, but I can't use add from instance of Children. How could I access __add method inherited from Parent class to Children class ?

p = Parent(2)
p.addition(4)

c = Children(5)
c.addition(4)

all these calls work, but the two below don't

p.__add(5)  # AttributeError: 'Parent' object has no attribute '__add'
c.add(4)  # AttributeError: 'Children' object has no attribute '_Children__add'

And both return absence of attribute. The first behavior is the expected one, I do not want to change, but the second one is disturbing to me, since a sub-class should be able to access its parent hidden method, shouldn't it ?

More details : in fact the parent class in my case are dealing with a DB connection, and deal with username and passwords, with some method to check the password __check_password that I prefer to let 'private'. I'd like to call __check_password from different child classes, that deal with data manipulation later.

FraSchelle
  • 249
  • 4
  • 10
  • 1
    `return self._Parent__add(y)`? See [name mangling](https://docs.python.org/3/tutorial/classes.html#private-variables). – Axe319 Oct 14 '22 at 11:10
  • 1
    The entire point of `__private` is to *prevent* access from child classes. While access is still possible, *why do you want this*? The simplest approach is just *not* to use a `__private` method but rather one that is `_protected` or `public`. – MisterMiyagi Oct 14 '22 at 11:31
  • @MisterMiyagi Thank you for your comment. I already explained _why I want this_ in my question. – FraSchelle Oct 14 '22 at 12:59
  • @FraSchelle The question currently just says you prefer to let `__check_password` be private then immediately say you prefer *not* to use it as private. I was hoping for an actual reason, not a restatement of the apparent contradiction. – MisterMiyagi Oct 14 '22 at 13:02
  • @MisterMiyagi I'd like the attribute/method to be hidden at the level of the Parent and the Children classes, yet to be transmissible from one to the other in a hidden way. Thank you for pointing me in the right normenclature :-) – FraSchelle Oct 14 '22 at 13:10
  • 1
    Does this answer your question? [What is the meaning of single and double underscore before an object name?](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-single-and-double-underscore-before-an-object-name) – MisterMiyagi Oct 14 '22 at 13:14
  • @MisterMiyagi Thank you, it indeed explain that I should not try to hide anything that way in Python. So the answer to my question is 'yes it's possible' ... – FraSchelle Oct 14 '22 at 13:37
  • ...and that's @Axe319 's comment. Thank you very much to both of you ! – FraSchelle Oct 14 '22 at 13:38

0 Answers0