I am improving this code to a student, here is the image:
So I rewrite this using list compreenssion and oriented to a formal class, so the second question is: Can I improve yet more this code?
JavaScript
x
33
33
1
class aluno():
2
def __init__(self):
3
# cria uma lista de alunos
4
self.id = 0
5
self.alunos = {}
6
7
def adiciona_aluno(self, name, info=None):
8
# cria um novo aluno com o id incremental
9
self.alunos[self.id] = {
10
'name':name,
11
'matricula':self.id,
12
'info': info if info else "Sem informação"
13
}
14
self.id += 1
15
16
def view(self,id):
17
#visualiza um aluno no formato especificado
18
aluno = self.alunos[id]
19
print(f'({aluno["matricula"]}) Nome:{aluno["name"]} - Info:{aluno["info"]}')
20
21
def view_all(self):
22
# itera em todos os alunos
23
[self.view(aluno) for aluno in self.alunos]
24
25
def main():
26
lista_de_alunos = aluno()
27
lista_de_alunos.adiciona_aluno("Franz")
28
lista_de_alunos.adiciona_aluno("Pedro", "Aluno exemplar em amatematica")
29
lista_de_alunos.view_all()
30
31
if __name__ == "__main__":
32
main()
33
Advertisement
Answer
Yes you can improve your code to be more “Object Oriented”.
It doesn’t make sence that Aluno contains a list, Aluno should represent a single student instance.
Below is a sample code. Note that my sample code can be improved, you can, for instances, add a Person class that stores common attributes to Student and Professor class, like birth date, id, etc.
JavaScript
1
70
70
1
class Aluno:
2
_id = 0
3
4
def __init__(self, name: str, age: int):
5
self.name = name
6
self.age = age
7
self.id = Aluno._id
8
Aluno._id += 1
9
10
def __cmp__(self, other):
11
return self.id == other.id
12
13
def __str__(self):
14
return f"- ID: {self.id}; - Name {self.name}; - Age: {self.age};"
15
16
17
class Professor(Aluno):
18
def __init__(self, name: str, age: int, wage: float):
19
super().__init__(name=name, age=age)
20
self.wage = wage
21
22
23
class Turma:
24
def __init__(self, year: int, letter: str, director: Professor, alunos: list = None):
25
if alunos is None:
26
alunos = []
27
28
self.year = year
29
self.letter = letter
30
self.director = director
31
self.alunos = alunos
32
33
def add_aluno(self, aluno):
34
# TODO Check if the aluno alerady exists (or work with a set)
35
self.alunos.append(aluno)
36
37
def remove_aluno(self, aluno):
38
# TODO Check if the aluno can be removed
39
self.alunos.remove(aluno)
40
41
def __str__(self):
42
_str = f"{self.year}{self.letter}n"
43
f"* Director:n"
44
f"- {self.director}n"
45
f"* Alunos:n"
46
for aluno in self.alunos:
47
_str += str(aluno)+"n"
48
return _str
49
50
51
if __name__ == "__main__":
52
aluno1 = Aluno(name="Foo Bar", age=18)
53
aluno2 = Aluno(name="Joe", age=16)
54
55
prof = Professor(name="Dr. Phill", age=54, wage=1256)
56
57
turma = Turma(year=10, letter="C", director=prof, alunos=[aluno1])
58
turma.add_aluno(aluno2)
59
60
print(f"ALUNO 1: {aluno1}")
61
print(f"ALUNO 2: {aluno2}")
62
63
print(f"PROFESSOR: {prof}")
64
65
print(f"TURMA: {turma}")
66
67
turma.remove_aluno(aluno2)
68
print(f"TURMA: {turma}")
69
70
Outputs:
JavaScript
1
17
17
1
ALUNO 1: - ID: 0; - Name Foo Bar; - Age: 18;
2
ALUNO 2: - ID: 1; - Name Joe; - Age: 16;
3
PROFESSOR: - ID: 2; - Name Dr. Phill; - Age: 54;
4
TURMA: 10C
5
* Director:
6
- - ID: 2; - Name Dr. Phill; - Age: 54;
7
* Alunos:
8
- ID: 0; - Name Foo Bar; - Age: 18;
9
- ID: 1; - Name Joe; - Age: 16;
10
11
TURMA: 10C
12
* Director:
13
- - ID: 2; - Name Dr. Phill; - Age: 54;
14
* Alunos:
15
- ID: 0; - Name Foo Bar; - Age: 18;
16
17