literals optimization (was Re: append on lists)

Maric Michaud maric at aristote.info
Tue Sep 16 12:07:03 EDT 2008


Le Tuesday 16 September 2008 16:57:26 Grant Edwards, vous avez écrit :
> On 2008-09-16, Maric Michaud <maric at aristote.info> wrote:
> > Le Tuesday 16 September 2008 15:57:53 Grant Edwards, vous avez écrit :
> >> On 2008-09-16, Maric Michaud <maric at aristote.info> wrote:
> >> > all expressions that return something, return a new object,
> >>
> >> That's not _quite_ true:
> >> >>> a=1
> >> >>> b=a.__add__(0)
> >> >>> a is b
> >>
> >> True
> >>
> >> ;)
> >
> > This is implementation specific,
>
> Indeed.
>
> > the specification of the language says that it should be
> > false,
>
> That's curious.  If so, then the implimentation is in violating
> the specification.

But it doesn't matter as the "is" operator is not relevant for any use case 
with scalars and even strings and the optimization is not negligible.
In fact, I'd rather say it's unspecified, each implementation does it its own 
way.

maric at redflag1 17:48:03:~$ ipython

>>>[1]: int() is 0 # contructor honour cache
...[1]: True

>>>[2]: 256 is 255+1 # still caching
...[1]: True

>>>[3]: 257 is 255+2 # here it stops
...[3]: False

maric at redflag1 17:48:08:~$ jython
Jython 2.2.1 on java1.6.0_07
Type "copyright", "credits" or "license" for more information.
>>> int() is 0 # not caching when using constructor
0
>>> 2 is 1+1 # caching on expression
1
>>> 500 is 250*2 # caching occurs beyond my CPython
1
>>> 5000 is 5000 # compile-time optimization
1
>>> 5000 is 2500*2 # not caching too big numbers
0


> Where is that in the specification?

Each literal creates a new instance, and instances of int are not supposed to 
be unique for each value (at less it's not specified so), so "1 is not 1", 
but implementation can cache integers, short strings, to avoid creating an 
new instance each time they appear in code.

jython do this also for integer (see above), but not for strings :

maric at redflag1 17:33:52:~$ jython
Jython 2.2.1 on java1.6.0_07
Type "copyright", "credits" or "license" for more information.
>>> a = "aaaa"
>>> a is "aaaa"
0
maric at redflag1 17:36:48:~$ ipython

>>>[1]: a = "aaaa"

>>>[2]: a is "aaaa"
...[2]: True

BTW, I'm talking of Jython 2.2, Jython 2.4, which quite ready I think, can 
change his mind as well.

> I  
> suspect the statement in the specification should only apply to
> mutable objects.
>

No, CPython also have a unique empty tuple, but tuple literals are not cached 
nor there are compile-time optimization :

>>>[5]: () is ()
...[5]: True

>>>[6]: (1,) is (1,)
...[6]: False


> > and it is for higher numbers :
> >>>>[15]: a=1000
> >>>>
> >>>>[16]: b=a.__add__(0)
> >>>>
> >>>>[17]: a is b
> >
> > ...[17]: False
> >
> > Don't disturb our OP, with side questions, please, it was
> > enough hard like this ;)
>
> I'm not trying to confuse the OP, but to quote Einstein:
> "Everything should be made as simple as possible, but not
> simpler."



-- 
_____________

Maric Michaud



More information about the Python-list mailing list