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

2

What is the function of the name of the classes inserted inside the parentheses of the constructor __init__( ) , in classes Telefones , TiposTelefone and Agenda ?

  

This is just a code snippet from Chapter 10 of "Introduction to Programming with Python" by 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()

    ...
    
asked by anonymous 26.07.2018 / 21:59

1 answer

2

So 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 you have that idea.

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

Actually I find this code bad because this should be a composition where the agenda should have, among other things a phone list. An agenda, and this should be the most appropriate name of the class, not a single list, it has a unique list. So it's more stuff trying to force OOP where it does not fit.

Then in this code around, when calling the super() is calling the constructor of the parent class, in this case the ListaUnica and passing it the argument that it expects, which is a type. It is not a common data, it is a data that represents a type declared in your code, in this case Telefone . It will use the type as information to check if everything is ok.

I hope you understand the difference between the type and the object itself. If you do not get it, go back 3 houses on the board to learn the concepts correctly before you can see examples so you can judge what is going on.

    
26.07.2018 / 22:20