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


Groups > comp.lang.python > #196978

Re: Two aces up Python's sleeve

From dn <PythonList@DancesWithMice.info>
Newsgroups comp.lang.python
Subject Re: Two aces up Python's sleeve
Date 2024-11-08 13:07 +1300
Organization DWM
Message-ID <mailman.87.1731024481.4695.python-list@python.org> (permalink)
References (1 earlier) <vgg5dn$ij48$1@solani.org> <050c2ce9efd8442fb902ecc926afb1ee42fe6c34.camel@tilde.green> <vgihe5$9tsc$1@solani.org> <lp4sgdFg90oU1@mid.individual.net> <0c94311a-f3e4-4aac-93e8-9bf95c065dcf@DancesWithMice.info>

Show all headers | View raw


On 8/11/24 11:15, Greg Ewing via Python-list wrote:
> On 8/11/24 3:04 am, Mild Shock wrote:
>> This only works for small integers. I guess
>> this is because tagged pointers are used
>> nowadays ?
> 
> No, it's because integers in a certain small range are cached. Not sure 
> what the actual range is nowadays, it used to be something like -5 to 
> 256 I think.
> 
> BTW you have to be careful testing this, because the compiler sometimes 
> does constant folding, so you need to be sure it's actually computing 
> the numbers at run time.

Haven't seen the OP. Is the Newsgroup link forwarding to the email-list 
correctly?


Integer interning is indeed valid for -5 <= i <= 256
("it works on my machine"! see below)

 >>> a = 0; b = 0; c = 0; d = 0
 >>> while a is b:
...     print( a, b, end=" ", )
...     print( c, d, ) if c is d else print()
...     a += 1; b += 1; c -= 1; d -= 1
...
0 0 0 0
1 1 -1 -1
2 2 -2 -2
3 3 -3 -3
4 4 -4 -4
5 5 -5 -5
6 6
7 7
8 8
9 9
...
254 254
255 255
256 256
 >>>

Be aware that this is implementation-dependent and not guaranteed to 
hold forever.

  dn  ~  python
Python 3.12.7 (main, Oct  1 2024, 00:00:00) [GCC 13.3.1 20240913 (Red 
Hat 13.3.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.


See also https://docs.python.org/3/library/sys.html#sys.intern

Thus could decide what is interned for yourself:

a_string = sys.intern( str( 1000 ) )

-- 
Regards,
=dn

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


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