[Python-3000] Set literal

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Feb 4 00:15:22 CET 2008


Larry Hastings wrote:
> When compiling a mutable type with an immutable equivalent, does Python 
> generate create-empty-object/insert-each-item bytecodes, or does it 
> create an immutable constant then cast it to the appropriate type?

Last time I looked, for lists and dicts it creates an empty
object and adds items to it one at a time, and for tuples it
pushes all the items onto the stack and then uses a special
create-tuple bytecode.

For frozenset constructors it could use a similar technique as
for tuples. If set comprehensions were to produce frozen sets,
something else would need to be done, such as creating it
mutable initially and freezing it after construction, or
having a special bytecode that's allowed to add an item to
a frozenset.

> p.s. or maybe the create-empty-object takes an optional parameter of 
> "and reserve this much space inside"?

That wouldn't work for set comprehensions, since there's no
way of telling ahead of time how many items it will contain.
It mightn't help much anyway, given Python's proportional
reallocation strategy.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | Carpe post meridiem!          	  |
Christchurch, New Zealand	   | (I'm not a morning person.)          |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+


More information about the Python-3000 mailing list