import math
class Circulo():
def __init__(self):
super()
self.__raio = None
def get_perimetro(self):
return 2 * math.pi * self.raio
def get_area(self):
return math.pi * self.raio ** 2
@property
def raio(self):
return self.__raio
@raio.setter
def raio(self, x):
self.__raio = x
I have the class above and I want to encapsulate the access, so that dynamic attributes are not possible in the instance.
ex:
c = Circulo()
c.raio = 2 # ok
c.lado = 2 # AttributeError
I tried to block the dynamic attributes with getattr
and setattr
, but I was not successful.
def __getattr__(self, item):
if item in self.__dict__:
return self.__dict__[item]
else:
raise AttributeError('Paramentro ou atributo "%s" inexistente.' % item)
def __setattr__(self, key, value):
if key in self.__dict__:
self.__dict__[key] = value
else:
raise AttributeError('Paramentro ou atributo "%s" inexistente.' % key)