Python Forum
Why Pass Functions as arguments?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why Pass Functions as arguments?
#1
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
Reply
#2
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.
Reply
#3
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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.
Reply
#5
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
Reply
#6
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
Reply
#7
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.
Reply
#8
(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.
Reply
#9
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.
Reply
#10
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing writable arguments to functions. Assembler 11 922 Jan-15-2024, 11:32 PM
Last Post: sgrey
  How to pass encrypted pass to pyodbc script tester_V 0 851 Jul-27-2023, 12:40 AM
Last Post: tester_V
  Passing string functions as arguments Clunk_Head 3 1,244 Jun-15-2022, 06:00 AM
Last Post: Gribouillis
  Possible to dynamically pass arguments to a function? grimm1111 2 2,167 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  how to pass arguments between pythons scripts? electricDesire 2 2,154 Oct-19-2020, 07:19 PM
Last Post: electricDesire
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,548 Sep-07-2020, 08:02 AM
Last Post: perfringo
  How to pass multiple arguments into function Mekala 4 2,446 Jul-11-2020, 07:03 AM
Last Post: Mekala
  Pass Arguments to Function phillyfa 2 2,016 Mar-27-2020, 12:05 PM
Last Post: phillyfa
  Pass by reference vs Pass by value leodavinci1990 1 2,194 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  How to pass arguments to popen? zBernie 3 12,827 Jul-09-2018, 01:26 AM
Last Post: zBernie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020