Skip to content
Merged
Prev Previous commit
Next Next commit
Use dict comprehensions instead of dict+zip
Dict comprehensions are more readable and faster:

❯ ./python -m timeit -s 'import types; c = dict(zip("abcdefghi", iter(types.CellType, None)))' 'dict(zip(c, map(id, c.values())))'
100000 loops, best of 5: 3.15 usec per loop

❯ ./python -m timeit -s 'import types; c = dict(zip("abcdefghi", iter(types.CellType, None)))' '{name: id(cell) for name, cell in c.items()}'
100000 loops, best of 5: 2.86 usec per loop
  • Loading branch information
johnslavik committed Jan 18, 2026
commit 416c501a3806154b93c298d080e08e570ac17ba2
4 changes: 2 additions & 2 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ def __eq__(self, other):
and self.__forward_is_class__ == other.__forward_is_class__
# Two separate cells are always considered unequal in forward refs.
and (
dict(zip(self.__cell__, map(id, self.__cell__.values())))
== dict(zip(other.__cell__, map(id, other.__cell__.values())))
{name: id(cell) for name, cell in self.__cell__.items()}
== {name: id(cell) for name, cell in other.__cell__.items()}
if isinstance(self.__cell__, dict) and isinstance(other.__cell__, dict)
else self.__cell__ is other.__cell__
)
Expand Down
Loading