The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)
alister
alister.ware at ntlworld.com
Thu Mar 24 14:14:56 EDT 2016
On Thu, 24 Mar 2016 14:04:53 +0000, BartC wrote:
> On 24/03/2016 13:50, Steven D'Aprano wrote:
>> On Thu, 24 Mar 2016 02:24 pm, Chris Angelico wrote:
>>
>>
>>> This is how you're currently evaluating Python. Instead of starting
>>> with the most simple and obvious code
>>
>> One problem is that what counts as "simple and obvious" depends on what
>> you are used to. Coming from a background of Pascal, iterating over a
>> list like this:
>>
>> for i in range(len(mylist)):
>> print mylist[i]
>>
>> was both simple and obvious. It took me years to break myself of that
>> habit.
>>
>> Likewise clearing a list:
>>
>> for i in range(len(mylist)-1, -1, 0):
>> del mylist[i]
>
> That's wouldn't be I'd call clearing a list, more like destroying it
> completely!
>
> How would you actually clear a list by traversing it (ie. not just
> building a new one)?
>
> This doesn't work:
>
> for x in L:
> x=0
>
> as each x only refers to the value in each element of L, not the element
> itself (like the pass-by-reference problem).
>
> I'd presumably have to do:
>
> for i in range(len(L)):
> L[i]=0
close
I would suggest the following pastern is more "Pythonic" although
possibly overkill for this scenario
a=[1,2,3,4,5]
for i,x in enumerate(a):
a[i]=None
--
Practice yourself what you preach.
-- Titus Maccius Plautus
More information about the Python-list
mailing list