[Python-3000] [Python-Dev] Python 3000 Status Update (Long!)
Nick Coghlan
ncoghlan at gmail.com
Thu Jun 28 15:56:25 CEST 2007
Greg Ewing wrote:
> Russell E. Owen wrote:
>> I would personally be happy lose set comprehensions and just use
>> generator expressions for all comprehension-like tasks.
>
> One advantage of the comprehension syntaxes is that the
> body can be inlined instead of relegated to a lambda,
> saving the overhead of a Python function call per
> loop.
I'm not sure what you mean by "function call per loop" in this
paragraph. There is no function call per loop even when using a
generator expression - a generator function is implicit defined, and
then called once to instantiate the generator. Iterating over this
suspends and resumes the generating to retrieve each item, rather than
making a Python function call as such - is that behaviour what you were
referring to?
Regardless, what the list and set comprehension syntax saves you is that
instead of having to suspend/resume a generator multiple times while
iterating over it to fill the container, the implicitly defined function
instead creates and populates the desired container type directly. These
operations are also compiled to use special opcodes, so they should be
significantly faster than the corresponding pure Python code would be.
(I'd provide some timing figures, but my Py3k checkout is somewhat
stale, so the timeit module isn't working for me at the moment)
To get back to the original question, I believe the point of adding set
literal and comprehension syntax is to make it possible to easily speed
up membership tests for items in known groups - the existing list
literals are fast to create, but slow to search. Using a set literal
instead of a list literal is also a good way to make it explicit that
the order in which the items are added to the container is arbitrary and
coincidental, rather than having any significant meaning.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-3000
mailing list