From: JChG on 18 Apr 2010 20:34 Well I started learning Python last week, and in my first experiment I got caught when I changed: sieve = [ {1:True} for x in range(r)] to sieve = [{1:True}] * r I was expecting it to be equivalent to sieve = [{1:True},{1:True},...] but instead it's t = [{1:True}]; sieve = [t,t,...] Okay, I see this was discussed 13 years ago, and it's a deliberate choice. There are other ways to do this. But I'll still whine anyway...I'm not seeing where list repetition is particularly useful, except when you want independent objects, like my initialization of sieve above. Oh well, I guess I'll just avoid it.
From: Steven D'Aprano on 19 Apr 2010 00:48 On Sun, 18 Apr 2010 17:34:03 -0700, JChG wrote: > But I'll still whine anyway...I'm not seeing where list repetition is > particularly useful, except when you want independent objects, like my > initialization of sieve above. Or when the objects are immutable, like ints, strings or None. pre_allocated = [None]*100 while condition: i = get_index() pre_allocated[i] = get_result() -- Steven
|
Pages: 1 Prev: Default paranmeter for packed values Next: UnicodeEncodeError during repr() |