Skip to content

Commit d0da0d3

Browse files
committed
Criado atributo de instância complexo 'filhos'
Modificado o método __repr__ para exibir os filhos
1 parent 2e32fb9 commit d0da0d3

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

oo/pessoa.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
class Pessoa:
2-
def __init__(self, nome=None, idade=41):
2+
def __init__(self, *filhos, nome=None, idade=41):
33
self.nome = nome
44
self.idade = idade
5+
self.filhos = list(filhos)
56

67
def cumprimentar(self):
78
return f"Ola {id(self)}"
89

910
def __repr__(self):
10-
return f"{self.nome} tem {self.idade} anos"
11+
txtSaida = f"Meu nome é {self.nome} e tenho {self.idade} anos"
12+
13+
if self.filhos:
14+
txtSaida = txtSaida + f"\nTenho {len(self.filhos)} filhos:"
15+
for filho in self.filhos:
16+
txtSaida = txtSaida + f"\n==> {filho}"
17+
18+
return txtSaida
19+
20+
1121

1222

1323
if __name__ == '__main__':
14-
p1 = Pessoa('Xico')
15-
p2 = Pessoa('Arthur', 7)
16-
print(Pessoa.cumprimentar(p1))
17-
print(p1.cumprimentar())
18-
print(id(p1))
19-
20-
print(p1)
21-
print(p2)
24+
filhos = [('Arthur', 7), ('Alice', 0)]
25+
26+
pai = Pessoa(nome='Adao Oliveira', idade=41)
27+
28+
for filho in filhos:
29+
pai.filhos.append(Pessoa(nome=filho[0], idade=filho[1]))
30+
31+
print(pai)

0 commit comments

Comments
 (0)