1class HauntedMansion:
2 prefix = "spooky_" # so as to use it multiple times
3
4 def __init__(self, **kwargs):
5 for key, value in kwargs.items():
6 setattr(self, self.prefix + str(key), value)
7
8 def __getattr__(self, name):
9 # not defined nor does it begin with the prefix
10 return "Booooo, only ghosts here!"
11
12 def __setattr__(self, name, value):
13 if not name.startswith(self.prefix) and not name.startswith("__"):
14 object.__setattr__(self, self.prefix + name, value)
15 else:
16 object.__setattr__(self, name, value)
F
======================================================================
FAIL: test_haunted_mansion (test.TestHauntedMansion.test_haunted_mansion)
The mansion should be really spooky and haunted.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 22, in test_haunted_mansion
self.assertEqual(haunted_mansion.spooky_spooky_buttler, "Hehe, I said butt...")
AssertionError: 'Booooo, only ghosts here!' != 'Hehe, I said butt...'
- Booooo, only ghosts here!
+ Hehe, I said butt...
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
f | 1 | class HauntedMansion: | f | 1 | class HauntedMansion: |
2 | prefix = "spooky_" # so as to use it multiple times | 2 | prefix = "spooky_" # so as to use it multiple times | ||
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, self.prefix + str(key), value) | 6 | setattr(self, self.prefix + str(key), value) | ||
7 | 7 | ||||
8 | def __getattr__(self, name): | 8 | def __getattr__(self, name): | ||
9 | # not defined nor does it begin with the prefix | 9 | # not defined nor does it begin with the prefix | ||
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 | if not name.startswith(self.prefix) and not name.startswith("__"): | 13 | if not name.startswith(self.prefix) and not name.startswith("__"): | ||
t | 14 | object.__setattr__(self, self.__class__.prefix + name, value) | t | 14 | object.__setattr__(self, self.prefix + name, value) |
15 | else: | 15 | else: | ||
16 | object.__setattr__(self, name, value) | 16 | object.__setattr__(self, name, value) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | class HauntedMansion: | f | 1 | class HauntedMansion: |
n | 2 | prefix = "spooky_" #so as to use it multiple times | n | 2 | prefix = "spooky_" # so as to use it multiple times |
3 | |||||
3 | def __init__(self, **kwargs): | 4 | def __init__(self, **kwargs): | ||
4 | for key, value in kwargs.items(): | 5 | for key, value in kwargs.items(): | ||
n | 5 | setattr(self, self.prefix+str(key), value) | n | 6 | setattr(self, self.prefix + str(key), value) |
6 | 7 | ||||
7 | def __getattr__(self, name): | 8 | def __getattr__(self, name): | ||
t | 8 | return "Booooo, only ghosts here!" #not defined nor does it begin with the prefix | t | 9 | # not defined nor does it begin with the prefix |
10 | return "Booooo, only ghosts here!" | ||||
9 | 11 | ||||
10 | def __setattr__(self, name, value): | 12 | def __setattr__(self, name, value): | ||
11 | if not name.startswith(self.prefix) and not name.startswith("__"): | 13 | if not name.startswith(self.prefix) and not name.startswith("__"): | ||
12 | object.__setattr__(self, self.__class__.prefix + name, value) | 14 | object.__setattr__(self, self.__class__.prefix + name, value) | ||
13 | else: | 15 | else: | ||
14 | object.__setattr__(self, name, value) | 16 | object.__setattr__(self, name, value) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
02.11.2024 21:37