The function mult_two_add_three takes a number, multiplies it by two and adds three. We want to make this more flexible. First, change the name of the function to mult_x_add_y .
Sure, I’d love to. But how do I change the name of a function? Kinda confused here.
ETA: I got it now. It wanted me to literally change the name of the function in the code, which had been empty/overwritten with stuff from previous sections. Had to reset the section.
“Change the name of the function” means actually changing the word “two” and the word “three” on the wording of that function definition to the word “x” and the word “y” (and later changing the numbers within the function). So the code in the terminal will look like:
As to changing of functions names in general, I played around a little and tried to use what @terarockstar09318 mentioned, but got a syntax error. What I could do was create another function and define it as the old mult_x_add_y():
The bananas() function seems to use/pull the code within mult_x_add_y() without me having to write it again. However that looks more like I created a different function, with a different name, that does the same thing as mult_x_add_y() rather than actually changing its name (as we have done on the exercise, manually replacing the “two” and “three” by “x” and “y” on the first function definition).
Bear in mind for the lesson this issue refers to, renaming the function is literally changing the name of the function in the code so just edit the line as per the previous reply-
More generally in Python there may be a reason we wish to change the name of a function after it has been bound to a reference and it is not practical and likely to throw up errors if we changed the name of the function in the code itself. Say for example we used a function imported from a module (changing the name in the module is a very bad idea). Best practice for for the given case would be from module import function as name but for other purposes like backwards compatability (there are proper ways to deal with backwards compatability references too) we may wish to just add a new reference to the same object.
@terarockstar09318 is simply adding a new name to the original function (it alters nothing about the object itself). This is perfectly reasonable and the syntax shoulld be straightforward, I’m not certain what error you received.
def mult_x_add_y(number, x, y):
print(number*x + y)
mult_x_add_y(1, 3, 1)
Out: 4
bananas = mult_x_add_y # Just binds a new name to the same object
bananas(1, 3, 1)
Out: 4
print(mult_x_add_y is bananas) # Are these bound to the same object?
Out: True
Regarding your code (I changed the name of the variables so it was more obvious that the arguments you pass to bananas are then subsequently passed to mult_x_add_y, it doesn’t actually change anything) it’s the same as-
To rename a function simply edit the function name to desired name in the editor.
For example :
function():
body
::::
rename to :
function_renamed():
body
::::