I'd like certain instances to initialize with certain default attribute values depending on the initialization parameters of the object. I'm considering using a nested dictionary as a class attribute, but it feels convoluted for some reason. Is there a best practice for this type of situation?
class Shape:
metadata = {
3: {"names": ["Triangle", "Triforce"], "color": "sage"},
4: {"names": ["Square", "Box", "Cube"], "color": "dusty rose"},
12: {"names": ["Dodecagon", "Crude circle"], "color": "gold"}
}
colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
def __init__(self, sides, *names):
# All instances will certainly have the same attributes
self.sides = sides
self.names = list(names)
# Most attributes will have automatically generated values based on
# the init parameters
self.color = self.colors[sides % 7]
self.names += [str(sides) + "-gon"]
# But a few will have commonly recognized values which I'd like to set
# manually.
# This is the part I'm not sure how to handle
data = __class__.metadata.get(sides)
if data is not None:
self.names += data["names"]
self.color = data["color"]
I could add the custom values after creating the objects, but if I ever create another object with the same initialization parameters, it won't retain those custom values (i.e. I want all my Shape(3) objects to posses the name "Triangle").