interning in Python



This video explains the concept of interning in Python.

More awesome topics covered here:
Intermediate Python:
Functional Programming in Python:
Python Package Publishing:
Multithreading in Python:
Multiprocessing in Python:
Parallel Programming in Python:
Concurrent Programming in Python:
Dataclasses in Python:…

, ,

4 thoughts on “interning in Python

  1. I checked in the above example if comma or space is present then only it results false.
    It is not the case for increase in string size.
    for example:
    >>>a='abcefghijklmnopqrstuvwxyzghjwkjghsjuhgjsdgjsdfghjsdghjkusdfgh0123456789'

    >>>b='abcefghijklmnopqrstuvwxyzghjwkjghsjuhgjsdgjsdfghjsdghjkusdfgh0123456789'

    >>>a is b
    True

    May i know why it happens?

  2. May i know what is the exact condition, if it is not satisfied, python not look for previous string in memory.
    ————————————————————————————-
    I have some other doubts.
    In command prompt:

    >>> a,b='a-b','a-b'
    >>> a is b, id(a), id(b)
    (False, 13827360, 13826592)
    >>> del a
    >>> del b
    >>> a='a-b';b='a-b'
    >>> a is b, id(a), id(b)
    (True, 13826592, 13826592)
    >>>

    In jupyter notebook:

    a,b='a-b','a-b'
    a is b, id(a), id(b)
    (True, 83335520, 83335520)
    del a; del b
    a='a-b';b='a-b'
    a is b, id(a), id(b)
    (False, 83335576, 83333840)

    Both results are opposite. So i checked as below:

    I checked in command prompt,

    >>> id('ab'), id('a-b'), id('-')
    (9959968, 9960256, 9206432)
    >>> id('ab'), id('a-b'), id('-')
    (9959968, 9960736, 9206432)
    >>> id('ab'), id('a-b'), id('-')
    (9959968, 9960256, 9206432)
    >>> id('ab'), id('a-b'), id('-')
    (9959968, 9960736, 9206432)

    >>> id('a-b')
    9960256
    >>> id('a-b')
    9960256
    >>> id('a-b')
    9960256

    In the above example when i use comma operator, the value of id('a-b')
    repeats after one statement, but when we don't use it then the value of id('a-b') remains same.

    When i checked in jupyter notebook, the value of id('a-b') always change.
    May i know what is the reason?

    ————————————————
    Why does some operations result False ?

    >>> a='abc';b='xyz'
    >>> a is 'abc'
    True
    >>> b is 'xyz'
    True
    >>> 'abc'+'xyz' is 'abcxyz'
    True
    >>> a+b is'abcxyz'
    False
    >>> c = 'abcxyz'
    >>> c is a+b
    False
    >>> c is 'abcxyz'
    True
    >>>
    ————————————————-
    >>> a=257
    >>> b=257
    >>> a is b
    False
    >>> 257 is 257
    True

    >>> a=256
    >>> b=1
    >>> a+b is 257
    False
    >>> 256+1 is 257
    True
    >>> c=257
    >>> c is 257
    False
    >>> a+b is c
    False
    >>>
    —————————————————

Leave a Reply

Your email address will not be published. Required fields are marked *