Indirectly reference objects using a string

In this case you could use a list to refer to images, if you create your images in code then:

self.images = [Image(source="Image") for image in range(10)]

self.images[0].source = "New Image source"

Or if you use the design template to create your images then you’ll have to write them by hand first…

self.images = [self.image0, self.image1, self.image2, ..., self.image9]
for image in self.images:
  image.source = "Image"

Or…if you don’t fancy that

self.images = []
for component_name in dir(self):
    if "image" in component_name:
        self.images.append(getattr(self, component_name))

And if you have a different naming convention

self.images = []
for component_name in dir(self):
    component = getattr(self, component_name)
    if isinstance(component, Image):
        self.images.append(component)

3 Likes