How can I rename a function?

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.

How do I rename a function?

1 Like

when you define a function:

def example():

you can rename the function at the definition:

def rename():
1 Like

not get it yet? you mean ‘rename’ is a function? could u be more specific?

Given the def word defines a function, yes, rename is a function. The only way to rename a function is to change the code .

i mean , is ‘rename’ a syntax?
so we only rename by delete the old and write new name it? but every later called function will not be changed?

1 Like

no, its just the name of the function.

most IDE’s and text-editors support renaming functions and methods, which will find the callers which then get renamed as well

1 Like

I’m lost. I have no idea what they are asking to do?

if you want to change the name of the function after its definition , you can do this

def example():
print(“something”)

change the name of function

new_name = example

so, here you can call the function by using any name,for eg:-

new_name()
example()

2 Likes

I don’t understand anything anyone is talking about

Your code originally looks like this:

def mult_two_add_three(number):
  print(number*2 + 3)

“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:

def mult_x_add_y(number, x, y):
  print(number*x + y)

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():

def bananas(number, x, y): mult_x_add_y(number, x, y);

bananas(1, 3, 1);
mult_x_add_y(1, 3, 1);

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-

def mult_x_add_y(number, x, y):
  print(number*x + y)

def bananas(a, b, c):
  mult_x_add_y(a, b, c)

bananas is an entirely different function which just calls mult_x_add_y.