Estaba haciendo unas pruebas con python tratando de "forzar", por así decirlo el encapsulamiento en python, ya que en python no existe como tal los atributos privados:
import re
class Mixin(object):
__a = 1
def __getattribute__(self, name):
if re.match(f'_{ self.__class__.__name__ }__', name):
raise AttributeError(f"'{ self.__class__.__name__ }' object has no attribute '{ name }'")
return super().__getattribute__(name)
Mixin()._Mixin__a
Se supone que de esta manera no se pude acceder al atributo __a
así: _Mixin__a
.
Pero me da el siguiente error:
Traceback (most recent call last):
File "/home/lcteen/Documentos/Programming/Python/Practices/sss.py", line 24, in <module>
Mixin()._Mixin__a
File "/home/lcteen/Documentos/Programming/Python/Practices/sss.py", line 19, in __getattribute__
if re.match(f'_{ self.__class__.__name__ }__', name):
File "/home/lcteen/Documentos/Programming/Python/Practices/sss.py", line 19, in __getattribute__
if re.match(f'_{ self.__class__.__name__ }__', name):
File "/home/lcteen/Documentos/Programming/Python/Practices/sss.py", line 19, in __getattribute__
if re.match(f'_{ self.__class__.__name__ }__', name):
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
No entiendo que estoy haciendo mal, ni el porque sucede el error.