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)
‘’’
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.
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:
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?
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
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.