Skip to content

Commit c02fc18

Browse files
authored
Refactor: Avoid redundant list operations in ListLike
- Removed `list()` casts and addintion in `__add__` and `__radd__` methods of `ListLike` and `NamedListLike` containers.
1 parent d133eb9 commit c02fc18

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

panel/layout/base.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,12 @@ def __iadd__(self, other: Iterable[Any]) -> ListLike:
388388
def __add__(self, other: Iterable[Any]) -> ListLike:
389389
if isinstance(other, ListLike):
390390
other = other.objects
391-
else:
392-
other = list(other)
393-
return self.clone(*(self.objects+other))
391+
return self.clone(*self.objects, *other)
394392

395393
def __radd__(self, other: Iterable[Any]) -> ListLike:
396394
if isinstance(other, ListLike):
397395
other = other.objects
398-
else:
399-
other = list(other)
400-
return self.clone(*(other+self.objects))
396+
return self.clone(*other, *self.objects)
401397

402398
def __contains__(self, obj: Viewable) -> bool:
403399
return obj in self.objects
@@ -644,23 +640,23 @@ def __iadd__(self, other: Iterable[Any]) -> NamedListLike:
644640

645641
def __add__(self, other: Iterable[Any]) -> NamedListLike:
646642
if isinstance(other, NamedListLike):
647-
added = list(zip(other._names, other.objects))
643+
added = zip(other._names, other.objects)
648644
elif isinstance(other, ListLike):
649645
added = other.objects
650646
else:
651-
added = list(other)
652-
objects = list(zip(self._names, self.objects))
653-
return self.clone(*(objects+added))
647+
added = other
648+
objects = zip(self._names, self.objects)
649+
return self.clone(*objects, *added)
654650

655651
def __radd__(self, other: Iterable[Any]) -> NamedListLike:
656652
if isinstance(other, NamedListLike):
657-
added = list(zip(other._names, other.objects))
653+
added = zip(other._names, other.objects)
658654
elif isinstance(other, ListLike):
659655
added = other.objects
660656
else:
661-
added = list(other)
662-
objects = list(zip(self._names, self.objects))
663-
return self.clone(*(added+objects))
657+
added = other
658+
objects = zip(self._names, self.objects)
659+
return self.clone(*added, *objects)
664660

665661
def __setitem__(self, index: int | slice, panes: Iterable[Any]) -> None:
666662
new_objects = list(self)

0 commit comments

Comments
 (0)