fork(1) download
  1. import math
  2.  
  3. class Circulo():
  4. def __init__(self):
  5. super()
  6. self.__raio = None
  7.  
  8. def get_perimetro(self):
  9. return 2 * math.pi * self.raio
  10.  
  11. def get_area(self):
  12. return math.pi * self.raio ** 2
  13.  
  14. @property
  15. def raio(self):
  16. return self.__raio
  17.  
  18. @raio.setter
  19. def raio(self, x):
  20. self.__raio = x
  21.  
  22. def __setattr__(self, key, value):
  23. if not hasattr(self, key):
  24. raise TypeError("Não pode criar atributos para esta classe")
  25. object.__setattr__(self, key, value)
  26.  
  27. c = Circulo()
  28. c.raio = 2 # ok
  29. c.lado = 2 # AttributeError
Runtime error #stdin #stdout #stderr 0.01s 27720KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 27, in <module>
  File "./prog.py", line 6, in __init__
  File "./prog.py", line 24, in __setattr__
TypeError: Não pode criar atributos para esta classe