1class HauntedMansion:
2 HELPER = ("_data", "spooky_")
3
4 def __init__(self, **kwargs):
5 self._data = {} # "protected"
6 for key, value in kwargs.items():
7 self._data[key] = value
8
9 def __getattr__(self, name):
10 if self._pref_validator(name):
11 original_name = name[len(self.HELPER[1]):]
12 if original_name in self._data:
13 return self._data[original_name]
14 return "Booooo, only ghosts here!"
15
16 def __setattr__(self, name, value):
17 if name == self.HELPER[0]: # protect the actual data of the class
18 object.__setattr__(self, name, value) # prevents an endless cycle
19 else:
20 self._data[name] = value
21
22 def _pref_validator(self, name):
23 if len(name) < len(self.HELPER[1]):
24 return False
25 for i in range(len(self.HELPER[1])):
26 if name[i] != self.HELPER[1][i]:
27 return False
28 return True
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
02.11.2024 20:35
02.11.2024 20:33
02.11.2024 20:30