-
Notifications
You must be signed in to change notification settings - Fork 0
/
aula151.py
42 lines (30 loc) · 823 Bytes
/
aula151.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Funções decoradoras e decoradores com classes
def my_repr(self): #essa função pode estar fora ou dentro do add_repr
class_name = self.__class__.__name__
class_dict = self.__dict__
class_repr = f'classe: {class_name}({class_dict})'
return class_repr
def add_repr(cls):
cls.__repr__ = my_repr
return cls
@add_repr #decorador 'açucar sintatico'
class Team:
def __init__(self, name):
self.name = name
@add_repr
class Planet:
def __init__(self, name):
self.name = name
@add_repr
class Language:
def __init__(self, name):
self.name = name
brasil = Team('Brasil')
portugal = Team('Portugal')
earth = Planet('Terra')
mars = Planet('Marte')
portuguese = Language('Português')
spanish = Language('Espanhol')
print(brasil)
print(earth)
print(portuguese)