0

i knon that a lot of people asked the same question, but i litte bit confused, i have an abstract class with __init__

class FirstTry(ABC):
    def __init__(self) -> None:
        return NotImplemented

and a second class,

class SecondTry(FirstTry):
    def __init__(self, names: list, alias: dict) -> NoReturn:
        if names:
            self.names= names
        if alias:
            self.alies = alies
        self.names_count = None
        self.alias_alternatives = None
        super().__init__(names, alias)

but when i run my code i get this error

TypeError: FirstTry.__init__() takes 1 positional arguments but 3 were given
  • 2
    What do you *think* `super().__init__(names, alias)` does and why did you put that line of code in your program? Can you explain it? – Silvio Mayolo Mar 07 '22 at 00:53
  • i'm new, this line will take the suber init and redefine it with names and alias – Maria Moulinka Mar 07 '22 at 00:57
  • Related: [Python's super(), abstract base classes, and NotImplementedError](https://stackoverflow.com/q/4799401/2745495). Specifically, [this answer](https://stackoverflow.com/a/4799574/2745495): "*If the method is abstract, the concrete subclass does not call super.*" – Gino Mempin Mar 07 '22 at 00:58
  • Note, you should not be doing `return NotImplemented` – juanpa.arrivillaga Mar 07 '22 at 02:11

1 Answers1

0

The reason you're getting the error is that this line:

super().__init__(names, alias)

is the same as calling:

FirstTry.__init__(self, names, alias)

and FirstTry.__init__ only takes a self argument.

If you were going to call FirstTry.__init__ via super() you'd want to call it in a way that matches its signature:

super().__init__()

but although this doesn't provoke a TypeError, it doesn't do anything useful -- in fact, if your __init__ raised a NotImplementedError instead of returning it, you'd just get an uncaught exception.

It would be simpler to just not define or call FirstTry.__init__ at all, since it's serving no function in this code.

Samwise
  • 68,105
  • 3
  • 30
  • 44
  • but the FirstTry is a class shared by several classes that should have some functions – Maria Moulinka Mar 07 '22 at 01:07
  • That doesn't mean that it needs an `__init__`. – Samwise Mar 07 '22 at 01:07
  • how can i define the init in FirstTry and reuse it in all the classes – Maria Moulinka Mar 07 '22 at 01:08
  • The way I described in my answer -- however, the implementation you showed doesn't do anything useful, so there's nothing to reuse. There's no point having your child classes call an `__init__` function that doesn't initialize anything. – Samwise Mar 07 '22 at 01:08
  • Note that you don't need to explicitly add a constructor for the sole purpose of calling the parent's constructor. Regular old `object.__init__` will call `super().__init__()` for you. (You *do* however need to explicitly call the parent's constructor *if* you override `__init__` for something else, since then that's getting run *instead of* `object.__init__`!) – Samwise Mar 07 '22 at 01:15
  • @Samwise - I'm not sure I get what you're saying in your last comment. Maybe you can elaborate a bit. What is the first code block, and where do these things live in the class hierarchy? – CryptoFool Mar 07 '22 at 01:23
  • @CryptoFool I have no idea what you're asking, sorry! If you post it as a question with a code sample I'll take a look. :) – Samwise Mar 07 '22 at 01:24
  • Note, `NotImplemented` and `NotImplementedError` are two different things, and `NotImplemented` should not be involved here – juanpa.arrivillaga Mar 07 '22 at 02:11