0

Imagine I have a two functions

def areaSquare(a,b):
   print( a * b)

def areaCircle(radius):
   print(3.14159 * radius ** 2)

And I want to create a third function that is called area.

area(areaCircle,radius = 3, repeat = 5)
# prints 3.14159 * 9 five times
area(areaSquare, a = 2, b = 3, repeat = 6)
# prints 2 * 6 six times

So the function takes a function as a parameter. Depending on the function which is passed to it as a parameter, it should require additional parameters. Is there a way to achieve this? I know function overloading would be an option. But I do not want to define multiple functions for this purpose.

MrYouMath
  • 547
  • 2
  • 13
  • 34
  • 1
    I wouldn't use functions but class in this case. Define a class Area with circle and square methods, where you would be able to pass your arguments elegantly. – Jean Rostan Apr 11 '18 at 19:48
  • 1
    you should check this -> https://stackoverflow.com/questions/6289646/python-function-as-a-function-argument – Liora Haydont Apr 11 '18 at 19:48
  • Possible duplicate of [Python function as a function argument?](https://stackoverflow.com/questions/6289646/python-function-as-a-function-argument) – user3483203 Apr 11 '18 at 19:49
  • @JeanRostan: Could you elaborate what you meant? What are the advantages of your method compared to the answer of Willem Van Onsem. – MrYouMath Apr 11 '18 at 20:07
  • @MrYouMath I would say readability of code, but it is probably a personal opinion. It also comes more naturally, Willem answer is perfect, but doesn't come to me as fast as custom methods for a class, which allows you to do lots of hacks cleanly, hidden behind classes. Nevertheless, his answer is interesting, but I would suggest you check on OOP (object oriented programming), it will eventually be useful to you. – Jean Rostan Apr 11 '18 at 20:16

1 Answers1

4

Yes, kwargs are your friend here. We can define the function such that the remaining parameters are captured in a dictionary named kwargs, and then passed to the function we provide. Like:

def area(func, repeat, **kwargs):
    for _ in range(repeat):
        func(**kwargs)

So all parameters except func and repeat are stored in the kwargs dictionary, and later we call the func function (the first argument) with the named parameters.

Note that this will not work (correctly) for functions that require a func and/or repeat parameter, since we capture these at the area function level.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • plus 1 For you quick answer. I will accept the answer as soon as I have tried it out for my problem. – MrYouMath Apr 11 '18 at 19:54
  • @MrYouMath: aarrghh.. the functions are not returning, but printing? Why? Usually you only want a very small subset to `print`. It is still possible, but very "unelegant". Anyway, I've made an edit such that it works now. – Willem Van Onsem Apr 11 '18 at 19:54
  • Thank you a lot! Your method worked like a charm. My functions were a little bit more involved that is why I tried to break it down such that it would be easier to answer. – MrYouMath Apr 11 '18 at 20:03
  • Just out of curiosity. How is this method called programming languages? And how would it generalize to multiple functions as parameters? – MrYouMath Apr 11 '18 at 20:05
  • 1
    Well in Python everything is an object (an integer is an object, a string is an object, an class instance is an object, a class itself is an object, etc). As a result you can pass those objects as parameters, and return them as parameters. Usually if functions are *first class citizens*, people call this *functional programming*. – Willem Van Onsem Apr 11 '18 at 20:06