0

I'm trying to call a method from the parent's class in the child class, but I get an exception.

That's the parent class:

from typing import Dict


class Parent:
    __arg: Dict

    @property
    def arg(self) -> Dict:
        return self.__arg

That's the child class:

class Child(Parent):
    def __init__(self, title) -> None:
        super(Child, self).__init__()
        self.__arg: Dict = {
            "some": "text",
        }

When I'm trying to run Child("Metallica").arg I get this exception: AttributeError: 'Child' object has no attribute '_Parent__arg'

Can someone help me please :3

more-suo
  • 3
  • 2
  • 1
    Why do you want to name the `Dict` in `Parent` `__arg`? – quamrana May 19 '22 at 19:39
  • @quamrana Dict is from typing, sorry for not mentioning it xD – more-suo May 19 '22 at 19:43
  • Yes, I realise that, but what about the name `__arg`? Why that name? – quamrana May 19 '22 at 19:44
  • @quamrana to make it private – more-suo May 19 '22 at 19:46
  • @more-suo **that does not make it private**. Python **doesn't have private variables**. this is *crucial* to understand. It does, however, have name-mangling, which serves the same purpose as a `private` access modifier for *one* particular use-case, it *prevents name collisions in the sublcass*. So if you *want* to use it in the subclass, then **dont use double-underscore name mangling**. Note, you would have the same problem if you use `private` in a class that actually had private variables – juanpa.arrivillaga May 19 '22 at 19:56
  • Soooo the solution is just to use a *single* underscore, which mean "internal part of the API touch at your own risk!". i.e. privacy by *convention*. Although, since you are just exposing this anyway, you might as well remove the `property` and just sue a regular attribute, `self.arg = {"some":"text"}` – juanpa.arrivillaga May 19 '22 at 20:07

0 Answers0