Re: Many newbie questions regarding python
- From: alex23 <wuwei23@xxxxxxxxx>
- Date: Thu, 7 Oct 2010 18:34:58 -0700 (PDT)
On Oct 8, 10:27 am, Steven D'Aprano <st...@REMOVE-THIS-
v = [0 for i in range(20)]
Absolutely not. Such a code snippet is very common, in fact I've done it
myself, but it is a "hammer solution" -- to a small boy with a hammer,
everything looks like a nail that needs hammering. Writing such a list
comprehension is a "list comp solution".
v =  * 20
Yes, this is the solution.
But the list comp version will work as expected for mutable types,
whereas the 'solution' only works for immutable types.
If anything, I feel like the list comp version is the correct solution
because of its reliability, whereas the multiplication form feels like
either a lucky naive approach or relies on the reader to know the type
of the initialising value and its mutability.