1class HauntedMansion:
2 __prefix = "spooky_"
3
4 def __init__(self, **kwargs):
5 for key, value in kwargs.items():
6 setattr(self, key, value)
7
8 def __getattr__(self, name):
9 return "Booooo, only ghosts here!"
10
11 def __setattr__(self, name, value):
12 super().__setattr__(f"{self.__prefix}{name}", value)
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
f | 1 | class HauntedMansion: | f | 1 | class HauntedMansion: |
2 | __prefix = "spooky_" | 2 | __prefix = "spooky_" | ||
3 | 3 | ||||
4 | def __init__(self, **kwargs): | 4 | def __init__(self, **kwargs): | ||
5 | for key, value in kwargs.items(): | 5 | for key, value in kwargs.items(): | ||
6 | setattr(self, key, value) | 6 | setattr(self, key, value) | ||
7 | 7 | ||||
8 | def __getattr__(self, name): | 8 | def __getattr__(self, name): | ||
n | 9 | #if not name.startswith(self.__prefix) and not name.startswith("__"): | n | ||
10 | return "Booooo, only ghosts here!" | 9 | return "Booooo, only ghosts here!" | ||
11 | 10 | ||||
12 | def __setattr__(self, name, value): | 11 | def __setattr__(self, name, value): | ||
13 | super().__setattr__(f"{self.__prefix}{name}", value) | 12 | super().__setattr__(f"{self.__prefix}{name}", value) | ||
t | t | 13 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | class HauntedMansion: | f | 1 | class HauntedMansion: |
2 | __prefix = "spooky_" | 2 | __prefix = "spooky_" | ||
3 | 3 | ||||
4 | def __init__(self, **kwargs): | 4 | def __init__(self, **kwargs): | ||
5 | for key, value in kwargs.items(): | 5 | for key, value in kwargs.items(): | ||
6 | setattr(self, key, value) | 6 | setattr(self, key, value) | ||
7 | 7 | ||||
8 | def __getattr__(self, name): | 8 | def __getattr__(self, name): | ||
n | 9 | if not name.startswith(self.__prefix) and not name.startswith("__"): | n | 9 | #if not name.startswith(self.__prefix) and not name.startswith("__"): |
10 | return "Booooo, only ghosts here!" | 10 | return "Booooo, only ghosts here!" | ||
11 | 11 | ||||
12 | def __setattr__(self, name, value): | 12 | def __setattr__(self, name, value): | ||
13 | super().__setattr__(f"{self.__prefix}{name}", value) | 13 | super().__setattr__(f"{self.__prefix}{name}", value) | ||
t | 14 | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | class HauntedMansion: | f | 1 | class HauntedMansion: |
2 | __prefix = "spooky_" | 2 | __prefix = "spooky_" | ||
3 | 3 | ||||
4 | def __init__(self, **kwargs): | 4 | def __init__(self, **kwargs): | ||
5 | for key, value in kwargs.items(): | 5 | for key, value in kwargs.items(): | ||
6 | setattr(self, key, value) | 6 | setattr(self, key, value) | ||
7 | 7 | ||||
8 | def __getattr__(self, name): | 8 | def __getattr__(self, name): | ||
9 | if not name.startswith(self.__prefix) and not name.startswith("__"): | 9 | if not name.startswith(self.__prefix) and not name.startswith("__"): | ||
10 | return "Booooo, only ghosts here!" | 10 | return "Booooo, only ghosts here!" | ||
11 | 11 | ||||
t | 12 | t | |||
13 | def __setattr__(self, name, value): | 12 | def __setattr__(self, name, value): | ||
14 | super().__setattr__(f"{self.__prefix}{name}", value) | 13 | super().__setattr__(f"{self.__prefix}{name}", value) | ||
15 | 14 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
02.11.2024 21:34