Один из подходов - создать layer
с уже существующим composition
.Это позволит вам передавать composition
объект каждому layer
объекту во время создания.
class Layer:
def __init__(self, composition, name):
if not isinstance(composition, Composition):
raise TypeError(
'instance of the layer class cannot exist without an instance '
'of the composition class')
self.composition = composition
self.name = name
def __repr__(self):
return self.name
def get_composition_info(self):
return (
'composition [{}] with size [{} x {}] and layers {}'
.format(
self.composition.name,
self.composition.height,
self.composition.width,
self.composition.layers))
class Composition:
def __init__(self, name, height, width):
self.layers = list()
self.name = name
self.height = height
self.width = width
def __repr__(self):
return self.name
def create_layer(self, name):
layer = Layer(self, name)
self.layers.append(layer)
return layer
comp = Composition('my_composition_1', 10, 2)
l_1 = comp.create_layer('layer_1')
l_2 = comp.create_layer('layer_2')
print(comp)
print(comp.layers)
print(l_1)
print(l_1.get_composition_info())
Вывод print()
s:
my_composition_1
[layer_1, layer_2]
layer_1
composition [my_composition_1] with size [10 x 2] and layers [layer_1, layer_2]