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, item):
9 return "Booooo, only ghosts here!"
10
11 def __setattr__(self, name, value):
12 name = f"{HauntedMansion.__prefix}{name}"
13 super().__setattr__(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, item): | 8 | def __getattr__(self, item): | ||
t | 9 | if HauntedMansion.__prefix not in item: | t | ||
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 | name = f"{HauntedMansion.__prefix}{name}" | 12 | name = f"{HauntedMansion.__prefix}{name}" | ||
14 | super().__setattr__(name, value) | 13 | super().__setattr__(name, value) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
02.11.2024 21:33