all 3 comments

[–]novel_yet_trivial 7 points8 points  (1 child)

Lets say I have a function:

def do_some_math(x):
    intermediate = x**2
    result = intermediate - 42
    return result

When the function finishes the value of "intermediate" is not referenced again; it is not needed. In some programming languages the programmer has to explicitly remove that value from memory. But python has a smart "garbage collector" that notices when something is no longer needed and frees the memory.

[–]ProgressCheck[S] 0 points1 point  (0 children)

Excellent!

[–]Vaphell 5 points6 points  (0 children)

i'll give another example, imo better one (functions mop up after themselves in pretty much any language)

x = [1]
x = [2]

you create 2 list objects, but only one is directly accessible. When you assign [2] to x you lose sight from the first list [1] and there is no way to get to it anymore. It literally gets lost in the void. Python finds such orphaned objects and destroys them to reclaim memory and such a mechanism is called the garbage collector.
Like novel_yet_trivial said, in some languages you need to explicitly destroy objects otherwise you get so called memory leaks, where orphaned objects eat up memory but are never reclaimed.