Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #196976

Re: Two aces up Python's sleeve

From Mild Shock <janburse@fastmail.fm>
Newsgroups comp.lang.python
Subject Re: Two aces up Python's sleeve
Date 2024-11-07 15:04 +0100
Message-ID <vgihe5$9tsc$1@solani.org> (permalink)
References <seven-20241106014329@ram.dialup.fu-berlin.de> <vgg5dn$ij48$1@solani.org> <050c2ce9efd8442fb902ecc926afb1ee42fe6c34.camel@tilde.green>

Show all headers | View raw


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.
> 
> 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: Two aces up Python's sleeve Mild Shock <janburse@fastmail.fm> - 2024-11-06 17:27 +0100
  Re: Two aces up Python's sleeve Annada Behera <annada@tilde.green> - 2024-11-07 12:55 +0530
    Re: Two aces up Python's sleeve Mild Shock <janburse@fastmail.fm> - 2024-11-07 15:04 +0100
      Re: Two aces up Python's sleeve Greg Ewing <greg.ewing@canterbury.ac.nz> - 2024-11-08 11:15 +1300
        Re: Two aces up Python's sleeve dn <PythonList@DancesWithMice.info> - 2024-11-08 13:07 +1300
        Re: Two aces up Python's sleeve Mild Shock <janburse@fastmail.fm> - 2024-11-08 01:25 +0100
          Re: Two aces up Python's sleeve Mild Shock <janburse@fastmail.fm> - 2024-11-08 01:29 +0100
            Re: Two aces up Python's sleeve Mild Shock <janburse@fastmail.fm> - 2024-11-08 01:47 +0100
    Re: Two aces up Python's sleeve (Posting On Python-List Prohibited) Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-11-08 01:10 +0000
      Re: Two aces up Python's sleeve (Posting On Python-List Prohibited) Mild Shock <janburse@fastmail.fm> - 2024-11-08 02:40 +0100
        Re: Two aces up Python's sleeve (Posting On Python-List Prohibited) dn <PythonList@DancesWithMice.info> - 2024-11-09 08:09 +1300
          Re: Two aces up Python's sleeve (Posting On Python-List Prohibited) Mild Shock <janburse@fastmail.fm> - 2024-11-08 20:49 +0100
        Re: Two aces up Python's sleeve (Posting On Python-List Prohibited) Thomas Passin <list1@tompassin.net> - 2024-11-08 17:00 -0500

csiph-web