-1

In Interactive mode of python, When i say,

>>> mystr = 'abc'

we have object created in the current frame of type string with content 'abc'

Now, If i change the binding of mystr as shown below,

>>> mystr = 'def'

then, the name mystr will bind to a new object with the content 'def'.

We know that string are immutable objects, so object containing 'abc' gets unaffected.

In my machine it works like this:

>>> mystr = 'abc'
>>> id(mystr)
30868568
>>> mystr = 'def'
>>> id(mystr)
36585632
>>> mystr = 'abc'
>>> id(mystr)
30868568

My question:

How does Python environment deal with object containing 'abc' after new binding, Will it purged?

he1ix
  • 383
  • 2
  • 11
overexchange
  • 15,768
  • 30
  • 152
  • 347
  • 4
    This is an implementation detail, but in CPython, small strings are interned and `"abc"` won't be deleted when your reference is gone. (By the way, never name a variable `str`, as it shadows the builtin type object...) – Wooble Jul 08 '14 at 12:59
  • http://stackoverflow.com/questions/10622472/when-does-python-choose-to-intern-a-string might be helpful. – Wooble Jul 08 '14 at 13:03
  • you are right martijn – overexchange Jul 08 '14 at 13:08
  • @MartijnPieters After i read your answer [link](http://stackoverflow.com/questions/24245324/about-the-changing-id-of-a-python-immutable-string), I doubt , if this is the reason `Python can then perhaps reuse the same memory location for a new string object, if you re-run the same code.` . It could not be for my above case. i think `abc` is sitting in some common pool and then being re-used like java, which wooble pointed above – overexchange Jul 08 '14 at 15:03
  • @overexchange: `abc` is interned there, it qualified as a identifier string (letters, digits and underscores only). – Martijn Pieters Jul 08 '14 at 15:04
  • @MartijnPieters so why did you tag my question as already answered here [link](http://stackoverflow.com/questions/24245324/about-the-changing-id-of-a-python-immutable-string)? – overexchange Jul 08 '14 at 15:08
  • @overexchange: because my answer there explains in exhaustive detail all the ways CPython can end up reusing a string object. – Martijn Pieters Jul 08 '14 at 15:10
  • @MartijnPieters Yes you are right, what about purging part Does unused string(`abc`) stay forever? – overexchange Jul 08 '14 at 15:16
  • @overexchange: interned string objects stay around until the interpreter shuts down. Non-interned string literals stay around as long as the code object stays around; for the most part that means the module the function was defined in stays around until Python shuts down, so the code object and the constant stay around until then too. – Martijn Pieters Jul 08 '14 at 15:18

1 Answers1

0

Strings are mutable, references are not.

When you assign str to "def", the other string "abc" does not affects. Just str's content will change, and it will reference to new "def" object.

When you manipulate strings in python, java etc.. languages such taking substring, it will make a new string object.

-For extra information (performance tips) if you work with strings for manipulating them use StringBuilder.

eakgul
  • 3,658
  • 21
  • 33
  • OP already knows that strings are immutable in Python, and is only asking about what happens to the immutable string that's no longer referenced. And StringBuilder isn't even a Python thing; I have no idea why you'd mention Java at all. -1. – Wooble Jul 08 '14 at 13:12
  • As I said It does not referenced from str. If no one reference it any more to it will destroy by garbage collection. – eakgul Jul 08 '14 at 13:14