+ 41
What exactly is the point of assigning a function to a variable?
15 odpowiedzi
+ 80
Assigning a function to a variable is a very powerful technique in functional programming. You could have an algorithm which works with "a function" but if you are using a variable to call that function, you have the flexibility of assigning a different function at runtime. It is something similar to polymorphism.
+ 40
Another advantage is maintainability of the code. For example if function is called at multiple places and at later point of time if you want to change the name of the function then you have to change the name at all places. If function was assigned with variable then you have to change the name at only one place. I.e where ever assignment has done.
+ 21
@Aditya, take a look on this example given in the lessons:
def do_twice(func, x, y):
return func(func(x, y), func(x, y))
By assigning a function to a variable - in this case a reference - you can create a general function which is able to perform a task depending on what function you pass as argument. It is a good tool for a DRY code, since you'll be able to reusing some functions, just like "do_twice" from above example, that otherwise you would need to do many times.
+ 4
just take a simple example :
*Every person has 4 apple and 5 banana.
*total no. of persons is a user input (i am hardcoding it as 10 for simplicity)
totalPerson = 10;
def calculateApple(p):
return p*4;
def calculateBanana(p):
return p*5;
allApple = calculateapple(TotalPerson);
allBanana = calculateBanana(TotalPerson);
totalFruits = allApple + allBanana;
So in this example, I have assigned calculateApple() return value to allApple variable. The function will use the value of total persons to calculate total no. of apples and then it will return the result.
That returned value will be stored in allApple variable.
+ 4
Es un poco complicado de entender sería mejor en una PC e ir paso a paso con el intérprete y ver cómo se va ejercitando para poder entender mejor
+ 3
Assigning a function to a variable is so important because to reduce code redundancy, faster to program than to create or declare variable again and again . To reduce code
+ 2
By assigning the function to a variable helps us to be organize and makes an easy access to the function again and again, we dont need to write down the same line of codes everytime we need it.
+ 1
Пишите на русском пожалуйста
+ 1
It is very important to avoid duplication of codes. If you assign a function to a particular variable, you can call it again elsewhere instead of rewriting the whole function.
0
@James Can you please elaborate ?
- 1
Assigning a function to a variable is a very powerful technique in functional programming. You could have an algorithm which works with "a function" but if you are using a variable to call that function, you have the flexibility of assigning a different function at runtime. It is something similar to polymorphism.
- 1
It's useful for closures
- 2
the type of variable like a function so we can assign function to var
- 3
Assigning a function to a variable is a very powerful technique in functional programming. You could have an algorithm which works with "a function" but if you are using a variable to call that function, you have the flexibility of assigning a different function at runtime. It is something similar to polymorphism.
- 5
Another advantage is maintainability of the code. For example if function is called at multiple places and at later point of time if you want to change the name of the function then you have to change the name at all places. If function was assigned with variable then you have to change the name at only one place. I.e where ever assignment has dpne.