New submission from Anthony Long <antl...@gmail.com>:

http://docs.python.org/c-api/int.html

"The current implementation keeps an array of integer objects for all integers 
between -5 and 256, when you create an int in that range you actually just get 
back a reference to the existing object. So it should be possible to change the 
value of 1. I suspect the behaviour of Python in this case is undefined. :-)"

This paragraph should be changed to reflect that you can (by construction) 
mutate anything you want in C, and (as per suggestion of dmalcolm)

"The current implementatin consolidates integers in the range -5 to 256 
(inclusive) into singleton instances.  Do not manipulate the internal value of 
a PyIntObject after creation."

Also, the last line of that paragraph insinuates this functionality (caching of 
-5 to 256) is undocumented. I searched for a good while for an answer for this, 
and I didn't find one. If there is something written on the implementation 
details surrounding why '-5 is -5' works, while -6 is -6' wouldn't. 

If there is nothing written about this, I will put something together. My final 
question however which I have not been able to find an answer for, is: Is this 
even necessary functionality?

I encountered around 100 blog posts and a couple of stackoverflow questions 
about why this fails, and it seems like 1) a source of confusion 2) a point of 
ridicule. Is it really necessary?

>>> id(1)
4298196440
>>> a = 1
>>> id(a)
4298196440
>>> id(3000)
4320396376
>>> a = 3000
>>> id(a)
4320396160
>>>

----------
components: Library (Lib)
messages: 133769
nosy: antlong
priority: normal
severity: normal
status: open
title: Implementation question for (-5) - 256 caching, and doc update for 
c-api/int.html
type: behavior

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue11846>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to