0

I have the following function:

def second(first, a):
    # i want to return the results of first(a) in this function.

I just can't figure out how i can put values in (first(a)) without creating another function, or using lambda or any other modules? Any help is appreciated.

  • "Put values in `first(a)`"? The "example" code seems lacking.. anyway, an object only needs to be [callable](http://stackoverflow.com/questions/111234/what-is-a-callable-in-python) to respond to `()`, functions (and constructors) normally have this property but it's not exclusive .. – user2864740 Jan 28 '14 at 05:56
  • I think the user wants to know how to **call** that function? – justhalf Jan 28 '14 at 05:59
  • you question is not clear enought, first is already a function, which is defined somewhere right ? if yes why you can't just `return first(a)` – James Sapam Jan 28 '14 at 06:21

2 Answers2

0
>>> def first(a, *args, **kwargs):
...     print((a, args, kwargs))
...
>>> def second(func, a, *args, **kwargs):
...     return func(a, *args, **kwargs)
...
>>> second(first, 'hello', 'world', do='something')
('hello', ('world',), {'do': 'something'})
>>> second(int, 15)
15
>>> second(range, 2, 5)
range(2, 5)

You can omit *args or **kwargs part if you know you don't need additional arguments or keyword arguments for your functions.

SzieberthAdam
  • 3,999
  • 2
  • 23
  • 31
0

Maybe you could call the function first passing as parameter a, and then return the value it returned:

def second(first, a):
    return first(a)
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73