Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say i store a password in plain text in a variable called passWd as a string. How does python release this variable once i discard of it (for instance, with del passWd or passWd= 'new random data')?

Is the string stored as a byte-array meaning it can be overwritten in the memoryplace that it originally existed or is it a fixed set in a memory area which can't be modified and there for when assining a new value a new memory area is created and the old area is discareded but not overwritten by null?

I'm questioning how Python implements the safety of memory areas and would like to know more about it, mainly because i'm curious :)

From what i've gathered so far, using del (or __del__) causes the interpreter to not release memory areas of that variable automaticly which can cause issues, and also i'm not sure that del is so thurrow on deleting the values. But that's just from what i've gathered and not something in black or white :)

share|improve this question

closed as off topic by Journeyman Geek, Brad Patton, nerdwaller, KronoS, slhck Apr 9 '13 at 15:32

Questions on Super User are expected to relate to computer software or computer hardware within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

While many people may know the answer here on SU, this should be posted on Stack Overflow, I assume it'll be migrated soon. – nerdwaller Apr 9 '13 at 14:36
I didn't dare to ask on SO, since my question isn't "i need help with this code problem" it's more of, "how is the memory management implemented on lower levels" :) but i guess i can give SO a chance to not hate on me :P – Torxed Apr 9 '13 at 14:38
Just a suggestion for posting over there: use a valid identifier since pass is a keyword in Python and couldn't be used :) Most people over there are pretty reasonable. – nerdwaller Apr 9 '13 at 14:41
A good and a valid point! Thank you! :) Also, you are very reasonable! – Torxed Apr 9 '13 at 14:42
Next time please don't cross post. As @nerdwaller said, your question could have been migrated. – slhck Apr 9 '13 at 15:32
up vote 1 down vote accepted

the answer is implementation specific. from

under 5.6 Sequence Types, take note of "Notes #6" which explains that some implementations (CPython for one) do in-place string updates in memory, whereas others presumably treat the string as a immutable object, that when replaced/altered defines a new area in memory.

share|improve this answer
I was hoping that was the case, otherwise i would have needed to write some C code of my own to actually do that job, if python didn't have a workaround for it that is. Assuming Py2.6+ there are some neat stuff like in-place concationations occur which is great.. But how about deletions, are they handled in a way that the momory is actually cleared, replaced by null or is it a standard free() on a malloc allocation? According to StackOverflow it's just freed, making it a security risk and i would have to do passWd = '...' before freeing the memory area, and even then, can i be sure? – Torxed Apr 9 '13 at 14:53
python is an intrepreter langague and uses garbage collection as the default memory recovery mechanism. per objects that do not define __del__() are garbage collected, whereas objects that do are not collectable and appear in the gc.Garbage return and must be released manually. running gc.collect() will cause the system to try to start disposing unlinked objects in memory, but garbage collection is never an explicit thing, but a best effort attempt. – Frank Thomas Apr 9 '13 at 15:17

Not the answer you're looking for? Browse other questions tagged .