2
Good, I’m trying to learn python because it seems to me a language with some applicability interest in the most diverse areas (and interest me for Raspberry).
As I learn I like to develop something to apply the knowledge.
As such I am developing a system consisting of a dictionary of classes (Created Class) whose identifier of each class is the acronym received.
Each class consists of an array of students (Created Class) and an acronym that identifies it.
All classes have setters and getters for all attributes.
My problem is that whenever I add a new student to a class already created and inserted in the class array, it adds it to all classes contained in the array and I can’t find the focus of the problem.
The code of my class class is as follows::
class Turma:
nome = None
sigla = None
alunos = []
def __init__(self, _nome, _sigla):
self.nome = _nome
self.sigla = _sigla
def getNome(self):
return self.nome
def addAluno(self, _aluno):
self.alunos.append(_aluno)
def getSigla(self):
return self.sigla
def getAlunos(self):
return self.alunos
The code of my student class is as follows::
class Aluno:
nome = None
idade = None
numero = None
def __init__(self, _nome, _idade, _num):
self.nome = _nome
self.idade = _idade
self.numero = _num
def getNome(self):
return self.nome
def getNumero(self):
return self.numero
def getIdade(self):
return self.idade
def setNome(self, _nome):
self.nome = _nome
def setIdade(self, _idade):
self.idade = _idade
def setNome(self, _num):
self.numero = _num
In my system control file I have the following features:
turmas = {}
def createTurma():
nome = input("Insira o nome da turma: ")
sigla = input("Insira a sigla da turma: ")
turmas[sigla] = Turma(nome, sigla)
print(turmas);
print("Turma Adicionada com Sucesso!")
def listTurma():
sigla = input("Insira a sigla do curso: ")
print(turmas[sigla].getNome())
def addAlunoTurma():
num = 0
nome = input("Insira o nome do aluno: ")
idade = input("Insira a idade do aluno: ")
sigla = input("Insira a sigla da turma: ")
_aluno = Aluno(nome, idade, 0)
turmas[sigla].addAluno(_aluno)
def getAlunosByTurma():
sigla = input("Insira a sigla da turma: ")
teste = turmas[sigla].getAlunos()
print(len(teste))
for i in turmas[sigla].getAlunos():
print("Nome: " + i.getNome())
print("Numero: " + str(i.getNumero()))
print("--------------")
I detected this problem when the function getAlunosByTurma() because regardless of the acronym always prints all students.
Thank you!!
To know what is wrong there, it is necessary to see the code of the classes. The problem seems to be in the méotod
getSigla
. Over time, you’ll understand better Pythone seeing that explicit getters and setters are not needed in most cases - and when they are, they work implicitly - without having to be called as methods (properties).– jsbueno
Thank you for the answer! I already complemented a little more my doubt so that it is possible to have a better idea of the problem and changed the array for a dictionary, to be simpler to reach the class I want!
– caoc10