Skip to content

[Feature]: Add __iter__() method to LayoutElements and Elements for native iteration support #436

@pira998

Description

@pira998

Description

Both LayoutElements (in layoutelement.py) and Elements (in elements.py )are structured to behave like containers or array-like objects. They expose methods like .slice() and hold list/array-like data (texts, element_coords, etc.).

However, neither class implements the __iter__() method. Attempting to loop over them directly results in:

TypeError: 'LayoutElements' object is not iterable

or similarly for Elements.

Suggested Fix

Add the following to both classes:

def __iter__(self):
    return self.iter_elements()

This will enable intuitive usage like:

for el in layout_elements:
    ...

or:

for el in elements:
    ...

Motivation

This change improves the developer experience and aligns both LayoutElements and Elements with Python's collection protocol. It supports clean and readable code, especially when using list comprehensions or loops.

Benefits

  • ✅ Enables native iteration with for el in elements:
  • ✅ Enhances clarity and developer-friendliness
  • 🚫 No breaking changes (backward-compatible)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions