4646
4747# ATRIBUTO DINAMICO
4848
49+ # class Pessoa():
50+ # def __init__(self, *filhos, nome=None, idade=35):
51+ # self.nome = nome
52+ # self.idade = idade
53+ # self.filhos = list(filhos)
54+ #
55+ #
56+ # def cumprimentar(self):
57+ # return f'Olá {id(self)}'
58+ #
59+ #
60+ # if __name__ == '__main__':
61+ # renzo = Pessoa(nome='Renzo')
62+ # luciano = Pessoa(renzo, nome='Luciano')
63+ # print(Pessoa.cumprimentar(luciano))
64+ # print(id(luciano))
65+ # print(luciano.cumprimentar())
66+ # print(luciano.nome)
67+ # print(luciano.idade)
68+ # for filho in luciano.filhos:
69+ # print(filho.nome)
70+ # luciano.sobrenome = 'Ramanlho' # Criação do atributo dinâmico fazendo a atribuição
71+ # del luciano.filhos # Remoção do atributo
72+ # print(luciano.__dict__) # Os atributos de instancia ficam presentes no __dict__
73+ # print(renzo.__dict__)
74+
75+ # ATRIBUTO DE CLASSE
76+
77+ # class Pessoa():
78+ # olhos = 2 # criado atributo de classe olhos
79+ #
80+ # def __init__(self, *filhos, nome=None, idade=35):
81+ # self.nome = nome
82+ # self.idade = idade
83+ # self.filhos = list(filhos)
84+ #
85+ # def cumprimentar(self):
86+ # return f'Olá {id(self)}'
87+ #
88+ #
89+ # if __name__ == '__main__':
90+ # renzo = Pessoa(nome='Renzo')
91+ # luciano = Pessoa(renzo, nome='Luciano')
92+ # print(Pessoa.cumprimentar(luciano))
93+ # print(id(luciano))
94+ # print(luciano.cumprimentar())
95+ # print(luciano.nome)
96+ # print(luciano.idade)
97+ # for filho in luciano.filhos:
98+ # print(filho.nome)
99+ # luciano.sobrenome = 'Ramanlho'
100+ # del luciano.filhos
101+ # luciano.olhos = 1
102+ # del luciano.olhos
103+ # print(luciano.__dict__)
104+ # print(renzo.__dict__)
105+ # Pessoa.olhos = 3
106+ # print(Pessoa.olhos)
107+ # print(luciano.olhos)
108+ # print(renzo.olhos)
109+ # print(id(Pessoa.olhos), id(luciano.olhos), id(renzo.olhos))
110+
111+ # MÉTODO DE CLASSE
112+
49113class Pessoa ():
114+ olhos = 2 # criado atributo de classe olhos
115+
50116 def __init__ (self , * filhos , nome = None , idade = 35 ):
51117 self .nome = nome
52118 self .idade = idade
53119 self .filhos = list (filhos )
54120
55-
56121 def cumprimentar (self ):
57122 return f'Olá { id (self )} '
58123
124+ @staticmethod
125+ def metodo_estatico ():
126+ return 42
127+
128+ @classmethod
129+ def nome_e_atributo_de_classe (cls ):
130+ return f'{ cls } - olhos { cls .olhos } '
131+
132+
59133
60134if __name__ == '__main__' :
61135 renzo = Pessoa (nome = 'Renzo' )
@@ -67,14 +141,18 @@ def cumprimentar(self):
67141 print (luciano .idade )
68142 for filho in luciano .filhos :
69143 print (filho .nome )
70- luciano .sobrenome = 'Ramanlho' # Criação do atributo dinâmico fazendo a atribuição
71- del luciano .filhos # Remoção do atributo
72- print (luciano .__dict__ ) # Os atributos de instancia ficam presentes no __dict__
144+ luciano .sobrenome = 'Ramanlho'
145+ del luciano .filhos
146+ luciano .olhos = 1
147+ del luciano .olhos
148+ print (luciano .__dict__ )
73149 print (renzo .__dict__ )
74-
75-
76-
77-
150+ Pessoa .olhos = 3
151+ print (Pessoa .olhos )
152+ print (luciano .olhos )
153+ print (renzo .olhos )
154+ print (id (Pessoa .olhos ), id (luciano .olhos ), id (renzo .olhos ))
155+ print (Pessoa .nome_e_atributo_de_classe (), luciano .metodo_estatico ())
78156
79157
80158
0 commit comments