Real spawning often needs small tweaks: the same prototype but a different name or tags. Implement spawn(name, **overrides): clone the registered prototype, then apply each override to the clone with setattr.
The prototype and any other clones must stay unchanged.
import copy
class Entity:
def __init__(self, name, tags=None):
self.name = name
self.tags = list(tags) if tags is not None else []
def clone(self):
"""Return an independent deep copy of this entity."""
return copy.deepcopy(self)
class PrototypeRegistry:
def __init__(self):
self._prototypes = {}
def register(self, name, prototype):
self._prototypes[name] = prototype
def create(self, name):
if name not in self._prototypes:
raise KeyError(name)
return self._prototypes[name].clone()
def spawn(self, name, /, **overrides):
"""Clone the prototype, then apply attribute overrides to the clone."""
# TODO: create a clone, setattr each override on it, return the clone
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Real spawning often needs small tweaks: the same prototype but a different name or tags. Implement spawn(name, **overrides): clone the registered prototype, then apply each override to the clone with setattr.
The prototype and any other clones must stay unchanged.
Press Run Tests or ββ© to check your solution.