I've been searching for the accurate answer to this question for a couple of days now but haven't got anything good. I'm not a complete beginner in programming, but not yet even on the intermediate level.
When I'm in the shell of Python, I type: dir()
and I can see all the names of all the objects in the current scope (main block), there are 6 of them:
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
Then, when I'm declaring a variable, for example x = 10
, it automatically adds to that lists of objects under built-in module dir()
, and when I type dir()
again, it shows now:
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'x']
The same goes for functions, classes and so on.
How do I delete all those new objects without erasing the standard 6 which where available at the beginning?
I've read here about "memory cleaning", "cleaning of the console", which erases all the text from the command prompt window:
>>> import sys
>>> clear = lambda: os.system('cls')
>>> clear()
But all this has nothing to do with what I'm trying to achieve, it doesn't clean out all used objects.
Just like the del method, you can invoke the gc. collect() for clearing the memory of not just variables but for all Python objects. Thus, you can use a combination of del() and gc. collect() to clear the variable from Python memory.
The del statement removes the variable from the namespace, but it does not necessarily clear it from memory. Therefore, after deleting the variable using the del statement, we can use the gc. collect() method to clear the variable from memory.
Delete a variable You can also delete Python variables using the command del “variable name”. In the below example of Python delete variable, we deleted variable f, and when we proceed to print it, we get error “variable name is not defined” which means you have deleted the variable.
The del statement works by unbinding the name, removing it from the set of names known to the Python interpreter. If this variable was the last remaining reference to an object, the object will be removed from memory. If, on the other hand, other variables still refer to this object, the object won't be deleted.
You can delete individual names with del
:
del x
or you can remove them from the globals()
object:
for name in dir():
if not name.startswith('_'):
del globals()[name]
This is just an example loop; it defensively only deletes names that do not start with an underscore, making a (not unreasoned) assumption that you only used names without an underscore at the start in your interpreter. You could use a hard-coded list of names to keep instead (whitelisting) if you really wanted to be thorough. There is no built-in function to do the clearing for you, other than just exit and restart the interpreter.
Modules you've imported (import os
) are going to remain imported because they are referenced by sys.modules
; subsequent imports will reuse the already imported module object. You just won't have a reference to them in your current global namespace.
Python doesn’t make any security guarantees about data in memory however. When objects no longer are referenced the interpreter marks the memory as no longer in use but does not take steps to overwrite that memory to prevent access to data. If you need that level of security protection you’ll need to use third-party extensions that manage their own memory with security in mind.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With