Instead of cloning entities by hand, keep a registry of named prototypes. register(name, prototype) stores a prototype; create(name) returns a fresh clone of the stored prototype.
Implement create(). If the name is unknown, raise KeyError.
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):
"""Return a fresh clone of the registered prototype."""
# TODO: look up name, raise KeyError if missing, else return a 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.
Instead of cloning entities by hand, keep a registry of named prototypes. register(name, prototype) stores a prototype; create(name) returns a fresh clone of the stored prototype.
Implement create(). If the name is unknown, raise KeyError.
Press Run Tests or ββ© to check your solution.