[Python-ideas] Star assignment in iterator way?
Steven D'Aprano
steve at pearwood.info
Tue Nov 21 05:53:38 EST 2017
On Tue, Nov 21, 2017 at 12:27:32PM +0300, Kirill Balunov wrote:
> Backward compatibility is an important issue, but at the same time it is
> the main brake on progress.
"Progress just means bad things happen faster."
-- Terry Pratchett, "Witches Abroad"
[...]
> And how do you look at something like this (deferred star evaluation)?:
>
> a, ?*b, c, d = something_iterable
A waste of effort?
How do you defer evaluating the second and subsequent items if you
evaluate the final two? Given:
def gen():
yield 999
for i in range(100):
yield random.random()
yield 999
yield 999
then
a, ?*b, c, d = gen()
has to evaluate all 100 random numbers in order to assign a, c, d all
equal to 999. Making b an iterator instead of a list doesn't actually
avoid evaluating anything, and it will still require as much storage as
a list. The most likely implementation would:
- store the evaluated items in a list;
- assign iter(the list) as b.
I suppose that there could be some way of delaying the calls to
random.random() by returning a thunk, but that is likely to be more
expensive in both memory and time than a simple list of floats.
--
Steven
More information about the Python-ideas
mailing list