[New-bugs-announce] [issue46310] Simplify `for` loop in `.close()` method in `asyncio/windows_events`
Nikita Sobolev
report at bugs.python.org
Sun Jan 9 04:11:22 EST 2022
New submission from Nikita Sobolev <mail at sobolevn.me>:
This line https://github.com/python/cpython/blob/3d11c1b8b49800c5c4c295953cc3abf577f6065a/Lib/asyncio/windows_events.py#L842 uses `.items()`, but the key seems unsused.
It would be better to use `.values()` there.
1. It is slightly faster, very generic benchmark:
```
import timeit
cache = {i: (i, i + 1, None, f'a{i}') for i in range(1000)}
timeit.timeit('list(cache.items())', number=1000, globals=locals())
# 0.18165644304826856
timeit.timeit('list(cache.values())', number=1000, globals=locals())
# 0.04786261299159378
```
2. It is more readable, because unused variables are not created
And since it does not break anything, I think it is safe to refactor.
----------
assignee: sobolevn
components: Library (Lib)
messages: 410136
nosy: asvetlov, eric.araujo, sobolevn
priority: normal
severity: normal
status: open
title: Simplify `for` loop in `.close()` method in `asyncio/windows_events`
type: behavior
versions: Python 3.11
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46310>
_______________________________________
More information about the New-bugs-announce
mailing list