1class HauntedMansion:
2 def __init__(self, **kwargs):
3 for key, value in kwargs.items():
4 self.__setattr__(key, value) # Calls bottom method/function
5
6 def __getattr__(self, name):
7 if not name.startswith("spooky_"):
8 return "Booooo, only ghosts here!" # Not a valid attribute
9
10 original_name = name[len("spooky_"):] # Get attribute names in dict after "spooky_"
11 if f"spooky_{original_name}" in self.__dict__:
12 return self.__dict__[f"spooky_{original_name}"] # Returns valid attribute
13
14 return "Booooo, only ghosts here!" # Not a valid attribute
15
16 def __setattr__(self, name, value):
17 self.__dict__[f"spooky_{name}"] = value # Sets the attribute with prefix "spooky_"
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK