On Sat, Jan 2, 2021 at 7:56 AM Jonathan Fine <jfine2...@gmail.com> wrote:
>
> Bravo, Jeff. I couldn't have chosen a better example.
>
> However, I'd expect large ints to be stored in two parts. A (class, pointer) 
> pair which has fixed size. And memory referenced by the pointer, large enough 
> to store the value of the large int.
>
> However, that's part of the language implementation, and not part of the 
> language definition.
>

Off-topic for python-ideas, but a bit of explanation...

It doesn't matter how they're stored. The fact is that an integer -
whether small or large - is an object, and every object has an
identity, which can be tested with the "is" operator and the id()
function. Python guarantees a few things about object identity:

1) Two distinct and concurrently-existing objects have different IDs.
2) Any given object always has the same ID.
3) Therefore "x is y" implies "id(x) == id(y)" and vice versa - as
long as both objects exist simultaneously.

There are a few things Python specifically doesn't guarantee:
1) Immutable and indistinguishable objects MAY be interned but may
also be distinct.
2) If an object can no longer be found in any way, its ID may be
reused. It's not possible to test "x is y" if you can't find x,
therefore it's not possible to disprove the "x is y implies id(x) ==
id(y)" statement.
3) IDs have no particular meaning. They're not addresses, they're not
sequential, they're just arbitrary numbers. (Some Python
implementations don't even assign ID numbers until they're requested,
which is fine as long as the three guarantees above are held.)

Any time you save an ID longer than you save the original object, you
open yourself up to the possibility of reuse - but depending on
exactly what code you run, you might not actually see it. And some
Python interpreters will never show that. OTOH, it's definitely
possible for integers, strings, tuples, etc to reuse the same object
even though they don't seem to need to; but there's absolutely no
guarantee:

Python 3.10.0a0 (heads/master:497126f7ea, Oct  2 2020, 21:22:13)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f1(): return id("Hello")
...
>>> def f2(): return id("Hello")
...
>>> def f3(): return id("Hello")
...
>>> print(f1(), f2(), f3())
140064512446192 140064512446192 140064512446192
>>> def f1(): return id("Hello world")
...
>>> def f2(): return id("Hello world")
...
>>> def f3(): return id("Hello world")
...
>>> print(f1(), f2(), f3())
140064512446064 140064512471728 140064512471536

CPython automatically interns *some* string literals, but not others
:) A compliant Python implementation is welcome to return the same ID
from each of these functions, or to return distinct IDs.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/WYYITCKUBYGPU6QU4BSAC6TPKCE5B2E6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to