1class HauntedMansion:
2 """HauntedMansion class needs a spooky_ var prefix for access to its named attributes."""
3 __BOO__ = "Booooo, only ghosts here!"
4 __SPOOKY__ = "spooky_"
5
6 def __init__(self, **kwargs):
7 for name, value in kwargs.items():
8 setattr(self, name, value)
9
10 def __getattr__(self, name):
11 if name.startswith(self.__SPOOKY__) and name in self.__dict__:
12 return self.__dict__.get(name)
13 return self.__BOO__
14
15 def __setattr__(self, name, value):
16 object.__setattr__(self, self.__SPOOKY__ + name, value)
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
f | 1 | class HauntedMansion: | f | 1 | class HauntedMansion: |
2 | """HauntedMansion class needs a spooky_ var prefix for access to its named attributes.""" | 2 | """HauntedMansion class needs a spooky_ var prefix for access to its named attributes.""" | ||
3 | __BOO__ = "Booooo, only ghosts here!" | 3 | __BOO__ = "Booooo, only ghosts here!" | ||
4 | __SPOOKY__ = "spooky_" | 4 | __SPOOKY__ = "spooky_" | ||
5 | 5 | ||||
6 | def __init__(self, **kwargs): | 6 | def __init__(self, **kwargs): | ||
7 | for name, value in kwargs.items(): | 7 | for name, value in kwargs.items(): | ||
8 | setattr(self, name, value) | 8 | setattr(self, name, value) | ||
t | 9 | t | 9 | ||
10 | def __getattr__(self, name): | 10 | def __getattr__(self, name): | ||
11 | if name.startswith(self.__SPOOKY__) and name in self.__dict__: | 11 | if name.startswith(self.__SPOOKY__) and name in self.__dict__: | ||
12 | return self.__dict__.get(name) | 12 | return self.__dict__.get(name) | ||
13 | return self.__BOO__ | 13 | return self.__BOO__ | ||
14 | 14 | ||||
15 | def __setattr__(self, name, value): | 15 | def __setattr__(self, name, value): | ||
16 | object.__setattr__(self, self.__SPOOKY__ + name, value) | 16 | object.__setattr__(self, self.__SPOOKY__ + name, value) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | class HauntedMansion: | f | 1 | class HauntedMansion: |
2 | """HauntedMansion class needs a spooky_ var prefix for access to its named attributes.""" | 2 | """HauntedMansion class needs a spooky_ var prefix for access to its named attributes.""" | ||
3 | __BOO__ = "Booooo, only ghosts here!" | 3 | __BOO__ = "Booooo, only ghosts here!" | ||
4 | __SPOOKY__ = "spooky_" | 4 | __SPOOKY__ = "spooky_" | ||
5 | 5 | ||||
6 | def __init__(self, **kwargs): | 6 | def __init__(self, **kwargs): | ||
7 | for name, value in kwargs.items(): | 7 | for name, value in kwargs.items(): | ||
8 | setattr(self, name, value) | 8 | setattr(self, name, value) | ||
9 | 9 | ||||
10 | def __getattr__(self, name): | 10 | def __getattr__(self, name): | ||
n | 11 | if name.startswith("spooky_") and name in self.__dict__: | n | 11 | if name.startswith(self.__SPOOKY__) and name in self.__dict__: |
12 | return self.__dict__.get(name) | 12 | return self.__dict__.get(name) | ||
n | 13 | return self.__class__.__BOO__ | n | 13 | return self.__BOO__ |
14 | 14 | ||||
15 | def __setattr__(self, name, value): | 15 | def __setattr__(self, name, value): | ||
t | 16 | object.__setattr__(self, self.__class__.__SPOOKY__ + name, value) | t | 16 | object.__setattr__(self, self.__SPOOKY__ + name, value) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
02.11.2024 21:26
02.11.2024 21:27