Posts: 138
Threads: 26
Joined: Nov 2020
Nov-30-2020, 04:49 PM
(This post was last modified: Nov-30-2020, 04:49 PM by muzikman.)
Greetings,
I am starting to learn Python coming from other OOP languages and I am experiencing a little confusion on the topic of passing functions as arguments. My question is:
Why pass a function as an argument, when you can just call the function from inside of the calling function?
I will probably learn why this unorthodox syntax will benefit me down the road as I learn more.
I do realize that some list functions such as map and filter take functions as a parameter, and there is no way around this. So, I am forced to do it the Python way.
Any help would be much appreciated.
Matt
Posts: 1,838
Threads: 2
Joined: Apr 2017
Think about why you pass anything to a function as an argument: so that the function can work with different data. By passing a function as an argument, you produce an abstraction that can be reused in different ways: filter for example takes a function that specifies which items to keep and the function you pass to map lets you vary how to transform the items. The general ideas of filtering and mapping are independent of the specific functions that are applied and that's represented by making those higher order functions. This sort of idea can arise in other contexts too, of course.
Posts: 8,154
Threads: 160
Joined: Sep 2016
the funny part with your examples is list comprehension is preferred over filter or map , incl. by Guido (e.g. https://www.artima.com/weblogs/viewpost....read=98196).
Anyway, consider for example sorted() function, that takes a one-argument function as key argument. This allows to have any custom sorts as long as you provide a function that is used to extract a comparison key from each element.
Posts: 138
Threads: 26
Joined: Nov 2020
Nov-30-2020, 08:18 PM
(This post was last modified: Nov-30-2020, 08:18 PM by muzikman.)
I just started learning Python so I'm not at comprehension yet.
I do see where you are coming from, especially using decorators. That makes more sense to me. However, you could still apply the non python syntax to decorators as well. In a class, you could just override it outside you could just call it from the function.
When passing a function as an argument, it is easier to read and to modify right there on the spot when using lambda functions as args; the self documenting technique of not only your naming conventions but what you're passing as arguments.
Posts: 1,358
Threads: 2
Joined: May 2019
It's a level of flexibility.
def multiply(a,b):
return a*b
def addit(a,b):
return a+b
def calcit(func,op1,op2):
return func(op1,op2)
function = multiply
a = 5
b = 6
print(calcit(function,a,b))
function = addit
print(calcit(function,a,b)) Output: 30
11
Posts: 138
Threads: 26
Joined: Nov 2020
Dec-01-2020, 01:50 PM
(This post was last modified: Dec-01-2020, 01:50 PM by muzikman.)
Thank you for your example. Yes, I understand it.
If I were to call these functions by name from the calcit function, I would have to change the code in that function. Instead of just assigning the new function outside the main function. Thank you everyone for your help.
(Nov-30-2020, 11:47 PM)jefsummers Wrote: It's a level of flexibility.
def multiply(a,b):
return a*b
def addit(a,b):
return a+b
def calcit(func,op1,op2):
return func(op1,op2)
function = multiply
a = 5
b = 6
print(calcit(function,a,b))
function = addit
print(calcit(function,a,b)) Output: 30
11
Posts: 138
Threads: 26
Joined: Nov 2020
What's cool about passing functions as arguments is: when working with lists or other collection types, the function you pass into, let's say, map, will take in an argument of type List.
Posts: 1,838
Threads: 2
Joined: Apr 2017
(Dec-01-2020, 02:33 PM)muzikman Wrote: when working with lists or other collection types, the function you pass into, let's say, map, will take in an argument of type List.
This isn't correct in general. The function you pass to map should take an argument of the type of elements that are in the collection - remember that map applies the function to each element in the collection.
Posts: 138
Threads: 26
Joined: Nov 2020
That is correct. I am learning Python and that isn't really what I meant to say.
(Jan-03-2021, 07:49 AM)ndc85430 Wrote: (Dec-01-2020, 02:33 PM)muzikman Wrote: when working with lists or other collection types, the function you pass into, let's say, map, will take in an argument of type List.
This isn't correct in general. The function you pass to map should take an argument of the type of elements that are in the collection - remember that map applies the function to each element in the collection.
Posts: 138
Threads: 26
Joined: Nov 2020
Here is one thing I don't understand. In other languages, you would pass a function as an argument like so, using brackets around the parameters:
print(calcit(function(a,b))) In Python you pass it with no parenthesis around the arguments:
print(calcit(function,a,b)) Is this the same thing in Python?
|