• Two aces up Python's sleeve

    From Stefan Ram@3:633/280.2 to All on Wed Nov 6 11:49:15 2024
    ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
    last_item = my_list[ -1 ]
    Way cleaner than my_list[ len( my_list )- 1 ], don't you think?

    In "The Mental Game of Python," Raymond Hettinger spills the
    beans about our noggins only being able to juggle 7 +/- 2
    things in our short-term memory.

    So, "last_item = my_list[ -1 ]" might still make the cut,
    while "my_list[ len( my_list)- 1 ]" could be biting off
    more than we can chew.

    |The problem is, the number of brain registers this uses is
    |10. This is no longer a decryption effort. This is a puzzle.
    |At the moment you put it together, you fully understand it.
    |But if this is embedded in bigger code, every time you hit
    |this line, you're going to have to pick apart "what does this
    |thing do?".
    Raymond Hettinger



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Mild Shock@3:633/280.2 to All on Thu Nov 7 03:27:37 2024
    Then please explain why I have to write:

    i += 1

    Instead of the shorter:

    i ++

    My short-term memory is really stressed.

    Stefan Ram schrieb:
    ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
    last_item = my_list[ -1 ]
    Way cleaner than my_list[ len( my_list )- 1 ], don't you think?

    In "The Mental Game of Python," Raymond Hettinger spills the
    beans about our noggins only being able to juggle 7 +/- 2
    things in our short-term memory.

    So, "last_item = my_list[ -1 ]" might still make the cut,
    while "my_list[ len( my_list)- 1 ]" could be biting off
    more than we can chew.

    |The problem is, the number of brain registers this uses is
    |10. This is no longer a decryption effort. This is a puzzle.
    |At the moment you put it together, you fully understand it.
    |But if this is embedded in bigger code, every time you hit
    |this line, you're going to have to pick apart "what does this
    |thing do?".
    Raymond Hettinger




    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Annada Behera@3:633/280.2 to All on Thu Nov 7 18:25:53 2024
    Then please explain why I have to write:

    =C2=A0=C2=A0=C2=A0 i +=3D 1

    Instead of the shorter:

    =C2=A0=C2=A0=C2=A0 i ++

    My short-term memory is really stressed.

    I heard this behavior is because python's integers are immutable.
    For example:

    >>> x,y =3D 5,5
    >>> id(x) =3D=3D 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.



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: tilde.green (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Thu Nov 7 22:03:29 2024
    Annada Behera <annada@tilde.green> wrote or quoted:
    5 is a object that x and y points to. ++x or x++ will redefine 5 to

    This doesn't add up for me. Using the same "logic,"
    you might as well say "x = x + 1" is off limits.



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Mild Shock@3:633/280.2 to All on Fri Nov 8 01:04:53 2024
    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.




    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Greg Ewing@3:633/280.2 to All on Fri Nov 8 09:15:38 2024
    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.

    --
    Greg

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From dn@3:633/280.2 to All on Fri Nov 8 11:07:48 2024
    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

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: DWM (3:633/280.2@fidonet)
  • From Mild Shock@3:633/280.2 to All on Fri Nov 8 11:25:52 2024
    Hi,

    In Java its possible to work this way
    with the Integer datatype, just call
    Integer.valueOf().

    I am not sure whether CPython does the
    same. Because it shows me the same behaviour
    for small integers that are more than

    only in the range -128 to 128. You can try yourself:

    Python 3.14.0a1 (tags/v3.14.0a1:8cdaca8, Oct 15 2024, 20:08:21)
    x,y = 10**10, 10**9*10
    id(x) == id(y)
    True

    Maybe the idea that objects have an address
    that can be accessed via id() has been abandoned.
    This is already seen in PyPy. So maybe we

    are falsly assuming that id() gives na object address.

    Greg Ewing schrieb:
    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.



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Mild Shock@3:633/280.2 to All on Fri Nov 8 11:29:48 2024

    For example this article:

    https://www.codementor.io/@arpitbhayani/python-caches-integers-16jih595jk

    about the integer singletons claims:

    x, y = 257, 257
    id(x) == id(y)
    False

    But on Windows my recent CPython doesn't do that:

    Python 3.14.0a1 (tags/v3.14.0a1:8cdaca8, Oct 15 2024, 20:08:21)
    x, y = 257, 257
    id(x) == id(y)
    True

    Mild Shock schrieb:
    Hi,

    In Java its possible to work this way
    with the Integer datatype, just call
    Integer.valueOf().

    I am not sure whether CPython does the
    same. Because it shows me the same behaviour
    for small integers that are more than

    only in the range -128 to 128. You can try yourself:

    Python 3.14.0a1 (tags/v3.14.0a1:8cdaca8, Oct 15 2024, 20:08:21)
    x,y = 10**10, 10**9*10
    id(x) == id(y)
    True

    Maybe the idea that objects have an address
    that can be accessed via id() has been abandoned.
    This is already seen in PyPy. So maybe we

    are falsly assuming that id() gives na object address.

    Greg Ewing schrieb:
    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.




    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Mild Shock@3:633/280.2 to All on Fri Nov 8 11:47:14 2024
    The wiked brain of ChatGPT gives me a lead:

    PEP 659
    Storing data caches before the bytecode.

    Maybe its an effect of constant folding
    and constant pooling by the compiler?

    Mild Shock schrieb:

    For example this article:

    https://www.codementor.io/@arpitbhayani/python-caches-integers-16jih595jk

    about the integer singletons claims:

    x, y = 257, 257
    id(x) == id(y)
    False

    But on Windows my recent CPython doesn't do that:

    Python 3.14.0a1 (tags/v3.14.0a1:8cdaca8, Oct 15 2024, 20:08:21)
    x, y = 257, 257
    id(x) == id(y)
    True

    Mild Shock schrieb:
    Hi,

    In Java its possible to work this way
    with the Integer datatype, just call
    Integer.valueOf().

    I am not sure whether CPython does the
    same. Because it shows me the same behaviour
    for small integers that are more than

    only in the range -128 to 128. You can try yourself:

    Python 3.14.0a1 (tags/v3.14.0a1:8cdaca8, Oct 15 2024, 20:08:21)
    x,y = 10**10, 10**9*10
    id(x) == id(y)
    True

    Maybe the idea that objects have an address
    that can be accessed via id() has been abandoned.
    This is already seen in PyPy. So maybe we

    are falsly assuming that id() gives na object address.

    Greg Ewing schrieb:
    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.





    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)