why generator assigned to slice?

2011-01-05 Thread ana sanchez





hi!!!

i found this when i read the source of a program in python:

self.__chunks[start:end] = (chunk for i in xrange(start, end))

and also this:

self.__lines[line:line] = (None for i in xrange(count))

what utility has to assign a generator to a slice???  ?the *final
result* isn't the same as this?:

self.__chunks[start:end] = [chunk for i in xrange(start, end)]

self.__chunks[line:line] = [None for i in xrange(count)]

thanks!!!

ana

p.d. excuse my english
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why generator assigned to slice?

2011-01-06 Thread ana sanchez
In  Peter Otten <__pete...@web.de> writes:

>ana sanchez wrote:

>> i found this when i read the source of a program in python:
>> 
>> self.__chunks[start:end] = (chunk for i in xrange(start, end))

>> what utility has to assign a generator to a slice???  ?the *final
>> result* isn't the same as this?:
>> 
>> self.__chunks[start:end] = [chunk for i in xrange(start, end)]

>Whoever used the first variant probably was hoping that it was either faster 
>or used less peak memory. I think the latter is wrong, and the former can 
>easily be checked:

>$ python -m timeit -s'chunk = "yadda"; chunks = range(100); start = 20; end 
>= 50' 'chunks[start:end] = (chunk for i in xrange(start, end))'
>10 loops, best of 3: 9.02 usec per loop

>$ python -m timeit -s'chunk = "yadda"; chunks = range(100); start = 20; end 
>= 50' 'chunks[start:end] = [chunk for i in xrange(start, end)]'
>10 loops, best of 3: 4.16 usec per loop

>$ python -m timeit -s'chunk = "yadda"; chunks = range(100); start = 20; end 
>= 50' 'chunks[start:end] = [chunk]*(end-start)'
>100 loops, best of 3: 1.02 usec per loop


peter thank you very much!!!  (i like very much the timing "miniscripts")

ana

-- 
http://mail.python.org/mailman/listinfo/python-list