Instantiating a class object in an Input

Asked

Viewed 208 times

0

I’m trying to make a program in python with two classes: Discipline and Room, at a certain point I want to relate the room registered in the class Room in the class Discipline, the code does not generate error, but at the end the room is not linked

class Sala:
    def __init__(self, numero):
        self.numero = numero
        self.andar = self.setAndar(numero)
        self.capacidade = None

    def setAndar(self, numero):
        self.andar = numero[0]
        return self.andar

    def set_capacidade(self, capacidade):
        self.capacidade = capacidade

    def get_sala(self):
        return self.numero

    def set_sala(self, sala):
        self.numero = sala

    def __str__(self):
        return "Sala: "+self.numero+" Andar: "+str(self.andar)+" Capacidade: "+self.capacidade

class Disciplina:
    def __init__(self, nome, semestre):
        self.nome = nome
        self.semestre = semestre
        self.sala = None

    def set_sala(self, sala):
        self.sala = sala

    def get_sala(self):
        if self.sala is None:
            return "Não definida!"
        if self.sala is not None:
            return self.sala

    def __str__(self):
        return "Disciplina: "+self.nome+" Semestre: "+self.semestre+" Sala : "+str(self.get_sala())


def relacionar_salas():

    imprime_disciplina()
    escolha_disciplina = input("Qual a disciplina gostaria de vincular a sala? ")
    objeto_discplina = classeDisciplina.Disciplina(escolha_disciplina, None)
    relatorio_salas()
    escolha_sala = input("A qual Sala? ")
    objeto_sala = classeDisciplina.Disciplina(escolha_sala, None)
    if escolha_disciplina not in lst_disciplina:
        print("Isso nao existe")
    else:
        objeto_discplina.set_sala(objeto_sala)
  • 1

    Why the objeto_sala is an instance of the class Disciplina?

  • 2

    A good answer to this question would have to go through a lot of concepts - it would have to be practically a (well-given) class of object orientation use. One place where perhaps the mix of "how to create related objects" is well explained can be the tutorial frameworks like Django, which map directly to related tables. I suggest you read the Django tutorial - the "models" part-even if you don’t want to do your web application right now. https://django-portuguese.readthedocs.io/en/1.0/intro/tutorial01.html - -

  • 2

    But warning - you are with more basic programming questions, from the example of the code you gave - practiment and writing random calls to functions, etc... may have to do more basic exercises before progressing to where you want to go.

  • Worth a look at article, must give the solution.

1 answer

0

Hello,

First it is very important to choose good names for functions. If the purpose of the function is to relate a room to a discipline, I suggest the name relaciona_sala_disciplina(). It sounds silly but a good name helps to know what a function does and what is the purpose of it.

Another point is that a function must have a single responsibility, so if its purpose is to relate a room to a discipline, it is not for it to create a room or a discipline, because that is not its purpose. This is the first principle of SOLID and applies classes but works very well with functions as well. I suggest you see this excellent video SOLID.

Let’s go to the code. First I modified the Magic methods __str__() of each class of

class Sala:
# ... varias linhas de código
   
    def __str__(self):
        return "Sala: "+self.numero+" Andar: "+str(self.andar)+" Capacidade: "+self.capacidade


class Disciplina:
# ... varias linhas de código

    def __str__(self):
        return "Disciplina: "+self.nome+" Semestre: "+self.semestre+" Sala: "+str(self.get_sala())

# ... varias linhas de código

for :

class Sala:
# ... varias linhas de código
    
    def __str__(self):
        return f"Sala: {self.numero}  Andar: {self.andar}  Capacidade: {self.capacidade}"


class Disciplina:
# ... varias linhas de código

    def __str__(self):
        return f"Disciplina: {self.nome}  Semestre: {self.semestre} Sala: {self.sala}"

# ... varias linhas de código

This way the print of each class is the same, but inside the method __str__ becomes more readable.

The first modification I made to the function relacionar_salas() was renamed her to relaciona_sala_disciplina().

disciplinas_disponiveis = ['matemática', 'física', 'química', 'biologia']

def relaciona_sala_disciplina():
    print(disciplinas_disponiveis)

    disciplina_escolhida = input("Qual a disciplina gostaria de vincular a sala? ")
    discplina = Disciplina(disciplina_escolhida, None)

    sala_escolhida = input("A qual Sala? ")
    sala = Sala(sala_escolhida)

    if disciplina_escolhida not in disciplinas_disponiveis:
        print("Isso nao existe")
    else:
        discplina.set_sala(sala.numero)
        return sala, discplina


sala_qualquer, disciplina_qualquer = relaciona_sala_disciplina()

print(sala_qualquer)
print(disciplina_qualquer)

Sequentially the process that will be carried out is the display of the available disciplines (I created this list disciplina_disponíveis just to simulate your function imprime_disciplina()). I also made other renames for example objeto_sala simply to sala. But in the end this function will create any two objects, one of type Sala and another of the kind Disciplina.

While running this code with the proper replacements I arrived at this output:

['matemática', 'física', 'química', 'biologia']
Qual a disciplina gostaria de vincular a sala? física
A qual Sala? 42
Sala: 42  Andar: 4  Capacidade: None
Disciplina: física   Semestre: None  Sala: 42

I hope very much that I have managed to help you. Any question do not hesitate to ask.

See you around!

Browser other questions tagged

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