[Python-3000] Set literals - another try
Josiah Carlson
jcarlson at uci.edu
Tue Aug 8 22:21:39 CEST 2006
"Collin Winter" <collinw at gmail.com> wrote:
>
> On 8/8/06, Josiah Carlson <jcarlson at uci.edu> wrote:
> > I personally don't see much of a use for set literals, considering that
> > there is a non-ambiguous spelling of it currently; set(...), whose only
> > cost above and beyond that of a set literal is a global name lookup.
>
> I thought one of the main arguments in favor of set literals is that a
> literal form would allow the compiler to perform optimisations that
> the set(...) spelling doesn't allow.
The optimization argument used to define language syntax seems a bit
like the "tail wagging the dog" cliche. For immutable literals that are
used a huge number of times (int, tuple, and other immutables), a
literal syntax for compiler optimization makes sense. But for mutables
(list, dict, etc.), literal syntax is more a convenience as than an
optimization, as the compiler hasn't historically created once and
copied for re-use, but pushed values on the stack and called the
relevant create list bytecode. [1]
- Josiah
[1]
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> def foo():
... return [1,2,3]
...
>>> def goo():
... return (1,2,3)
...
>>> dis.dis(foo)
2 0 LOAD_CONST 1 (1)
3 LOAD_CONST 2 (2)
6 LOAD_CONST 3 (3)
9 BUILD_LIST 3
12 RETURN_VALUE
>>> dis.dis(goo)
2 0 LOAD_CONST 4 ((1, 2, 3))
3 RETURN_VALUE
>>>
More information about the Python-3000
mailing list