Understanding the the "del" keyword

So I have a basic understanding of why this is occurring but would like a detailed explanation of this.

In the Introduction to Python, Functions: Intermediate - Step 1/12 when using the delete keyword, If the variable max_val_test_0 is printed after the created function max is deleted how come it still prints No max value returned instead of 10 in the output?

It’s it seems that max_val_test_0 can still store the value from the deleted max function since it is assigned the created function within the creation (define) and deletion of the created function. Below is an example:

a_list = [1, 8, 10, 9, 7]
print(max(a_list))

def max(list_name):
	return 'No max value returned'
	
max_val_test_0 = max(a_list)
print(max_val_test_0)
print(max(a_list))

del max

print(max_val_test_0)
print(max(a_list))

max_val_test_0 = max(a_list)
print(max_val_test_0)
print(max(a_list))

Here is the output from the executed code:

10                                                                                                                                   
No max value returned                                                                                                                
No max value returned                                                                                                                
No max value returned                                                                                                                
10                                                                                                                                  
10                                                                                                                                   
10
1 Like

Hey, Donnie. Insightful question!

Every object in Python has a home in your computer’s memory, this home has a specific address. You can find this address by passing the object to the built-in id function.

>>> a_list = [1, 8, 10, 9, 7]
>>> id(a_list)
140676641842432
>>> id(max) # This is the built-in `max` function
140676642640592
>>> def max(list_name):
...     return 'No max value returned'
... 
>>> id(max)
140676613816976

(If you run this on your computer, you’ll almost certainly get different results — that’s expected. If I repeat this experiment on my computer, I will also almost certainly get different results).

Note that id(max) changed after we created the knockoff version of max.

When you run max_val_test_0 = max(a_list), you are assigning the result of max(a_list) to max_val_test_0.

>>> max_val_test_0 = max(a_list)
>>> print(max_val_test_0)
No max value returned
>>> print(max(a_list))
No max value returned

But even before the assignment, something happens: max(a_list) is created. You can access its memory address by passing max_val_test_0 to id:

>>> id(max_val_test_0)
140676613962928

The object is created and it lives at 140676613962928. The variable name max_val_test_0 is simply a way to access this address. At this moment, this address is independent of the address of knockoff max, it’s an entity on its own.

When you delete knockoff max, you’re simply clearing the house that sits at 140676613816976, nothing happens to the object that lives in 140676613962928:

>>> del max
>>> print(max_val_test_0)
No max value returned
>>> id(max_val_test_0)
140676613962928
>>> id(max)
140676642640592

Note that the last output is the same address that I got when I ran id(max) at the beginning.

So naturally, the following behavior is expected:

print(max(a_list))
10

Continuing with your script:

>>> max_val_test_0 = max(a_list)
>>> print(max_val_test_0)
10
>>> print(max(a_list))
10

Now let’s check the address that max_val_test_0 refers to.

>>> id(max_val_test_0)
94877692666912

It’s something completely new!

I hope this helps.

3 Likes

Whoa! This is an amazing response @Bruno thank you! Your response is exactly what I thought and is superb in fully explaining what is happening at a deeper level!

1 Like