1import re
2
3
4class HauntedMansion:
5 def __init__(self, **kwargs):
6 for key in kwargs:
7 self.__dict__[key] = kwargs[key]
8
9 def __getattribute__(self, key):
10 if key == '__dict__' or key == '__class__':
11 return object.__getattribute__(self, key)
12 elif key in self.__dict__:
13 return "Booooo, only ghosts here!"
14 elif key.startswith('spooky'):
15 try:
16 return self.__dict__[re.sub(r"^spooky_", "", key)]
17 except KeyError:
18 return "Booooo, only ghosts here!"
19 else:
20 return "Booooo, only ghosts here!"
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
| n | n | 1 | import re | ||
| 2 | |||||
| 3 | |||||
| 1 | class HauntedMansion: | 4 | class HauntedMansion: | ||
| 2 | def __init__(self, **kwargs): | 5 | def __init__(self, **kwargs): | ||
| 3 | for key in kwargs: | 6 | for key in kwargs: | ||
| 4 | self.__dict__[key] = kwargs[key] | 7 | self.__dict__[key] = kwargs[key] | ||
| 5 | 8 | ||||
| 6 | def __getattribute__(self, key): | 9 | def __getattribute__(self, key): | ||
| 7 | if key == '__dict__' or key == '__class__': | 10 | if key == '__dict__' or key == '__class__': | ||
| 8 | return object.__getattribute__(self, key) | 11 | return object.__getattribute__(self, key) | ||
| 9 | elif key in self.__dict__: | 12 | elif key in self.__dict__: | ||
| 10 | return "Booooo, only ghosts here!" | 13 | return "Booooo, only ghosts here!" | ||
| 11 | elif key.startswith('spooky'): | 14 | elif key.startswith('spooky'): | ||
| t | 12 | (_, unspooky_part) = key.split('spooky_') | t | 15 | try: |
| 13 | return self.__dict__[unspooky_part] | 16 | return self.__dict__[re.sub(r"^spooky_", "", key)] | ||
| 17 | except KeyError: | ||||
| 18 | return "Booooo, only ghosts here!" | ||||
| 19 | else: | ||||
| 20 | return "Booooo, only ghosts here!" |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
02.11.2024 20:38
02.11.2024 20:41