What does the class name mean in super(). __init__()?

Asked

Viewed 608 times

2

How the name of the classes inserted within the constructor’s parentheses works __init__( ), in the classes Telefones, TiposTelefone and Agenda?

This is only an excerpt of Chapter 10 code from the book "Introduction to Programming with Python" by Author: Nilo Ney Coutinho Menezes.

class ListaUnica:

    def __init__(self, elem_class):
        self.lista = []
        self.elem_class = elem_class
    ...


class Telefone:

    def __init__(self, numero, tipo=None):
        self.numero = numero
        self.tipo = tipo
    ...


class Telefones(ListaUnica):

    def __init__(self):
        # Não entendo QUAL O FUNCIONAMENTO da classe Telefone
        # dentro do __init__ do método super(). Até onde estudei
        # sobre POO, a função construtora é usada para
        # inserir parâmetros, (ou não), para a classe, e normalmente
        # HERDAMOS ESSES PARÂMETROS da superclasse (no caso,
        # da classe ListaUnica) usando o método built-in super(), 
        # mas aqui nesse caso, inserimos a classe
        # Telefone no lugar de onde seria normalmente inserido parâmetros
        # que herdariamos da classe pai (no caso, ListaUnica),
        # acabei ficando sem entender o fluxo do código, 
        # poderia me explicar o que acontece com o código na linha abaixo...       
        super().__init__(Telefone)


class DadoAgenda:

    def __init__(self, nome):
        self.nome = nome
        self.telefones = Telefones()

    ...


class TiposTelefone(ListaUnica):

    def __init__(self):
        # não entendo qual o funcionamento da classe TiposTelefone
        # dentro do __init__ do metodo super() tbm!!!.
        super().__init__(TipoTelefone)


class Agenda(ListaUnica):

    def __init__(self):
        # não entendo qual o funcionamento da classe DadoAgenda
        # dentro do __init__ do metodo super() tbm!!!.
        super().__init__(DadoAgenda) 
        self.tiposTelefone = TiposTelefone()

    ...
  • You’re talking about the self, elem_class?

  • No, I’m talking about that name().init(Phone), do not know what works in the code!!!

  • What do you mean by functioning?

  • i mean is that I can not understand the pq and what makes the name of a class (in case, the name of the Phone class) be within the parentheses of the init of the super() method, up to where I studied POO, the super() method serves only to inherit the parameters of the parent class (in this case, the Listunica class), and in the place from which it is done (inherit parameters) is the name of a class, I did not understand the pq of it...

  • With the super().__init__() you will invoke the method __init__ of ListaUnica, you receive as a parameter elem_class. The class name here will be the value of elem_class

  • ok, so far so good, but in one should have been inserted the name elem_class instead of Phone, like: super().init(elem_class) ? , I don’t know why: super().init(Phone), because as far as I know about inheritance, the attributes are manipulated only between the parent class (Listaunica class) and the daughter class (Phones), so because of a class that has nothing to do with the two (Listaunica and Telefone) have been configured in the init() of the super method()?

Show 1 more comment

1 answer

2


From what I understand ListaUnica expects a type as a parameter to do some sort of check if the list is always receiving an element of the same type. In fact the code is bad and does not meet this requirement very well, but somehow has this idea.

Generally this is necessary to ensure that the list is homogeneous and therefore coherent; it is not a list of anything. Which shows that Python is not the most suitable language for this, although it works.

I actually think this code is bad because it should be a composition where the agenda should be, among other things a phone list. An agenda, and this should be the most appropriate name of the class, is not a single list, it has a single list. So it’s more of a material trying to force OOP where it doesn’t fit.

So in this code I turn, when it calls the super() is calling the mother-class builder, in case the ListaUnica and passing to him the argument she expects, which is a guy. It is not a common datum, it is a datum that represents a type declared in its code, in this case the Telefone. It will use the type as information to check that everything is ok.

I hope you understand the difference between the type and the object itself. If you don’t understand, go back 3 houses on the board to learn the concepts correctly before seeing examples, and so be able to judge what is happening.

  • That’s exactly what you said Maniero, the Listunica class is configured to accept only objects of a certain type, in fact this is just an excerpt of all the code that the book proposed to understand, but I still can’t understand how this line works: super().init(Phone), but all right, thanks for the help, I’ll see if I can understand by studying the code again, Valew!!!

  • 2

    In fact it could be a legal Pattern - but the type of subclasses of the Listaunica should be declared in a class attribute. The way it is, it’s pretty damn bad.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.