# Del unable to delete the function written

My Code:

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

def max(a_list):
return "No max value returned"

print(max(a_list))

del max
``````

What I expected to happen:

``````10
10

``````

What actually happened:

``````10
No max value returned

``````

The instructions of the exercise suggests that using the Del code, I will be able to delete max function I have written and return the built in python command of max(), however it doesn’t seem to work when I run the code. I wonder if I have used del incorrectly?

It seems that if I moved del code up, right after the function written as below (though this is not what the instruction asked) it will solve the problem. Appreciate further explanation on how del works, most examples I found are deleting variables and lists, I wonder if there is a difference when handling functions.

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

def max(a_list):
return "No max value returned"
del max

max_val_test_0 = max(a_list)
print(max_val_test_0)

``````

Let’s look at your code in chunks.

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

Here, `max` is just Python’s built-in function, so it should print 10, as you predicted.

Then we redefine `max`:

``````def max(a_list):
return "No max value returned"
``````

No when we use `max` now, it no longer is Python’s built-in function. It is the function we defined above which always returns `No max value returned`.

Hence, the following code line (`print(max(a_list))`) prints `No max value returned`.

Thus the output is. . .

``````10
No max value returned
``````

That you end the code with deleting `max` is no longer relevant.

1 Like

Thank you for your response, i missed a few lines of code earlier, this should be it

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

def max(a_list):
return “No max value returned”

max_val_test_0 = max(a_list)
print(max_val_test_0)

del max

‘’’
my suspicion is actually more on the position of Del and subsequent action. Upon reading further topics on local and global scope, I am guessing that Del at the end of running the function, deleted the built-in max() and retained the max function written hence no further results.

And if i tried moving Del up before running the function as below, it seems to be able to delete the function. Makes total sense except that it seems misleading that the dataquest instructions called for putting Del at the end to delete function.

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

def max(a_list):
return “No max value returned”

Del max

max_val_test_0 = max(a_list)
print(max_val_test_0)

‘’’
Sorry to confuse this back and forth, please let me know if my understanding is correct?

Can you please encode your code in two pairs of three backticks (```)? It will make it a lot easier to read which in turns helps people help you.

1 Like

No! It deleted the function we created (the one that always returns `No max value returned`).

We do this specifically to teach that when deleting the variable of a built-in function that was overwritten, you regain access to the built-in function:

1. Run the code `del max` to delete the `max()` function you wrote. This allows you to use the built-in `max()` function again.

I suspect I haven’t solved your confusion fully. If this is the case, can you please reformulate your question with all this new context in mind?

1 Like

Thanks Bruno this is clearer although I still have questions on the position of Del as it does not seem to have fully delete the written function even though we have regained built-in function.
Below I have now tested the effects of Del to reframe the question by printing max() in its built-in form and max() in a variable form after Del.

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

def max(a_list):
return "No max value returned"

max_val_test_0 = max(a_list)
print(max_val_test_0)

del max

print(max(a_list))
print(max_val_test_0)

``````

Expected results

``````10
No max value returned
10
10
``````

Actual results, I assumed if the written function was deleted, the variable of max() (last result) would have printed like the built in function returning 10, but it did not.

``````10
No max value returned
10
No max value returned
``````

Circling back to my point earlier, that perhaps to delete written function, we need to place the Del command before the written function is executed? Like this:

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

def max(a_list):
return "No max value returned"

del max

max_val_test_0 = max(a_list)
print(max_val_test_0)

print(max(a_list))
print(max_val_test_0)
``````

Actual results always returning 10 suggesting the function is now deleted

``````10
10
10
10
``````

Any thoughts?

Because only one of the print calls doesn’t given your expected result, we can reduce the question to this:

#### Why does ‘No max value returned’ get printed in the script below?

``````a_list = [1, 8, 10, 9, 7]

def max(a_list):
return "No max value returned"

max_val_test_0 = max(a_list)

del max

print(max_val_test_0)
``````

For the answer, I’ll switch context. Consider the following script. What is its output?

``````x = 3
y = x
x = 5
print(y)
``````
Click to reveal the output
``````3
``````

Is it what you expected? I suspect not because that’s the kind of error your incurring in your thought process.

When you assign the value of `max(a_list)` to `max_val_test_0`, the variable won’t be updated with modifications to `max`.

More theoretically, when you assign “something” to a variable, you’re telling the computer to go to an address in the computer’s memory that holds a raw value and get the value from there.

When you do `max_val_test_0 = max(a_list)`, the computer figures out what `max(a_list)` is and places the value (`"No max value returned"`) in the address and that’s what `max_val_test_0` gets.

Let me know if you have follow up questions.

1 Like

Oh I see! Variable stores return of function despite function being deleted. Enlightening, thank you.

1 Like