Re: Two aces up Python's sleeve

Liste des GroupesRevenir à cl python 
Sujet : Re: Two aces up Python's sleeve
De : janburse (at) *nospam* fastmail.fm (Mild Shock)
Groupes : comp.lang.python
Date : 07. Nov 2024, 15:04:53
Autres entêtes
Message-ID : <vgihe5$9tsc$1@solani.org>
References : 1 2 3
User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0 SeaMonkey/2.53.19
This only works for small integers. I guess
this is because tagged pointers are used
nowadays ? For large integers, also known
as bigint, it doesn't work:
Python 3.13.0a1 (tags/v3.13.0a1:ad056f0, Oct 13 2023, 09:51:17)
 >>> x, y = 5, 4+1
 >>> id(x) == id(y)
True
 >>> x, y = 10**200, 10**199*10
 >>> x == y
True
 >>> id(x) == id(y)
False
In tagged pointers a small integer is
directly inlined into the pointer. The
pointer has usually some higher bits,
that identify the type and when masking
to see the lower bits, one gets the
integer value.
But I don't know for sure whats going on,
would need to find a CPython documentation.
P.S.: I also tested PyPy it doesn't show
the same behaviour, because it computes
an exaberated id():
Python 3.10.14 (39dc8d3c85a7, Aug 27 2024, 14:33:33)
[PyPy 7.3.17 with MSC v.1929 64 bit (AMD64)]
 >>>> x, y = 5, 4+1
 >>>> id(x) == id(y)
True
 >>>> x, y = 10**200, 10**199*10
 >>>> id(x) == id(y)
True
 >>>> id(x)
1600000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000
000000000000000001
Quite funny!
Annada Behera schrieb:
Then please explain why I have to write:
>
     i += 1
>
Instead of the shorter:
>
     i ++
>
My short-term memory is really stressed.
 I heard this behavior is because python's integers are immutable.
For example:
      >>> x,y = 5,5
     >>> id(x) == id(y)
     True
 5 is a object that x and y points to. ++x or x++ will redefine 5 to
6, which the interpreter forbids to keep it's state mathematically
consistent. Also, by not supporting x++ and ++x, it avoids the pre-
and post-increment (substitute-increment v. increment-substitute) bugs
that plagues C and it's children.
 

Date Sujet#  Auteur
6 Nov 24 * Two aces up Python's sleeve15Stefan Ram
6 Nov 24 `* Re: Two aces up Python's sleeve14Mild Shock
7 Nov 24  `* Re: Two aces up Python's sleeve13Annada Behera
7 Nov 24   +- Re: Two aces up Python's sleeve1Stefan Ram
7 Nov 24   +* Re: Two aces up Python's sleeve6Mild Shock
8 Nov 24   i`* Re: Two aces up Python's sleeve5Greg Ewing
8 Nov 24   i +- Re: Two aces up Python's sleeve1dn
8 Nov 24   i `* Re: Two aces up Python's sleeve3Mild Shock
8 Nov 24   i  `* Re: Two aces up Python's sleeve2Mild Shock
8 Nov 24   i   `- Re: Two aces up Python's sleeve1Mild Shock
8 Nov 24   `* Re: Two aces up Python's sleeve (Posting On Python-List Prohibited)5Lawrence D'Oliveiro
8 Nov 24    `* Re: Two aces up Python's sleeve (Posting On Python-List Prohibited)4Mild Shock
8 Nov 24     +* Re: Two aces up Python's sleeve (Posting On Python-List Prohibited)2dn
8 Nov 24     i`- Re: Two aces up Python's sleeve (Posting On Python-List Prohibited)1Mild Shock
9 Nov 24     `- Re: Two aces up Python's sleeve (Posting On Python-List Prohibited)1Thomas Passin

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal