From 3656a20d282daa51997c2e6b34db77ce1e347204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Sun, 27 Nov 2022 11:52:33 -0300 Subject: [PATCH] =?UTF-8?q?Fun=C3=A7=C3=B5es=20decoradoras=20e=20decorador?= =?UTF-8?q?es=20com=20m=C3=A9todos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aula152.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 aula152.py diff --git a/aula152.py b/aula152.py new file mode 100644 index 0000000..f59292d --- /dev/null +++ b/aula152.py @@ -0,0 +1,54 @@ +# Funções decoradoras e decoradores com métodos + +def meu_repr(self): + class_name = self.__class__.__name__ + class_dict = self.__dict__ + class_repr = f'{class_name}({class_dict})' + return class_repr + + +def adiciona_repr(cls): + cls.__repr__ = meu_repr + return cls + + +def meu_planeta(metodo): + def interno(self, *args, **kwargs): + resultado = metodo(self, *args, **kwargs) + + if 'Terra' in resultado: + return 'Você está em casa' + return resultado + return interno + + +@adiciona_repr +class Time: + def __init__(self, nome): + self.nome = nome + + +@adiciona_repr +class Planeta: + def __init__(self, nome): + self.nome = nome + + @meu_planeta + def falar_nome(self): + return f'O planeta é {self.nome}' + + +brasil = Time('Brasil') +portugal = Time('Portugal') + +terra = Planeta('Terra') +marte = Planeta('Marte') + +print(brasil) +print(portugal) + +print(terra) +print(marte) + +print(terra.falar_nome()) +print(marte.falar_nome())