Created
January 6, 2019 15:10
-
-
Save Fhernd/35d1239534542dee78bfa8ebda1b642c to your computer and use it in GitHub Desktop.
Propiedad cálculo tardío. Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
class PropiedadTardia: | |
def __init__(self, funcion): | |
self.funcion = funcion | |
def __get__(self, instancia, clase): | |
if instancia is None: | |
return None | |
else: | |
valor = self.funcion(instancia) | |
setattr(instancia, self.funcion.__name__, valor) | |
return valor | |
class Circulo: | |
def __init__(self, radio): | |
self.radio = radio | |
@PropiedadTardia | |
def area(self): | |
print('Computando el área...') | |
return math.pi * self.radio ** 2 | |
@PropiedadTardia | |
def perimetro(self): | |
print('Computando el perímetro...') | |
return 2 * math.pi * self.radio | |
circulo = Circulo(3.0) | |
print(circulo.radio) | |
print(circulo.area) | |
print(circulo.area) | |
print(circulo.perimetro) | |
print(circulo.perimetro) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment