Python input function in function

Suppose I have some code like:

def myfunc(anotherfunc, extraArgs):
    # somehow call `anotherfunc` here, passing it the `extraArgs`
    pass

I want to pass another existing function as the anotherfunc argument, and a list or tuple of arguments as extraArgs, and have myfunc call the passed-in function with those arguments.

Is this possible? How do I do it - would I need exec/eval or similar?

asked Jun 9, 2011 at 7:47

1

Can a Python function be an argument of another function?

Yes.

def myfunc(anotherfunc, extraArgs):
    anotherfunc(*extraArgs)

To be more specific ... with various arguments ...

>>> def x(a,b):
...     print "param 1 %s param 2 %s" % (a,b)
... 
>>> def y(z,t):
...     z(*t)
... 
>>> y(x, ("hello","manuel"))
param 1 hello param 2 manuel

Python input function in function

wjandrea

24.8k8 gold badges53 silver badges73 bronze badges

answered Jun 9, 2011 at 7:48

Python input function in function

4

Here's another way using *args (and also optionally), **kwargs:

def a(x, y):
  print x, y

def b(other, function, *args, **kwargs):
  function(*args, **kwargs)
  print other

b('world', a, 'hello', 'dude')

Output

hello dude
world

Note that function, *args, **kwargs have to be in that order and have to be the last arguments to the function calling the function.

answered Sep 16, 2015 at 21:01

sabujpsabujp

9811 gold badge11 silver badges11 bronze badges

1

Functions in Python are first-class objects. But your function definition is a bit off.

def myfunc(anotherfunc, extraArgs, extraKwArgs):
  return anotherfunc(*extraArgs, **extraKwArgs)

answered Jun 9, 2011 at 7:50

Sure, that is why python implements the following methods where the first parameter is a function:

  • map(function, iterable, ...) - Apply function to every item of iterable and return a list of the results.
  • filter(function, iterable) - Construct a list from those elements of iterable for which function returns true.
  • reduce(function, iterable[,initializer]) - Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.
  • lambdas

answered Jun 9, 2011 at 8:09

Artsiom RudzenkaArtsiom Rudzenka

27k4 gold badges33 silver badges51 bronze badges

1

Function inside function: we can use the function as parameter too..

In other words, we can say an output of a function is also a reference for an object, see below how the output of inner function is referencing to the outside function like below..

def out_func(a):

  def in_func(b):
       print(a + b + b + 3)
  return in_func

obj = out_func(1)
print(obj(5))

the result will be.. 14

Hope this helps.

answered Feb 8, 2019 at 0:17

Python input function in function

TemizziTemizzi

3941 silver badge10 bronze badges

0

Decorators are very powerful in Python since it allows programmers to pass function as argument and can also define function inside another function.

def decorator(func):
      def insideFunction():
        print("This is inside function before execution")
        func()
      return insideFunction

def func():
    print("I am argument function")

func_obj = decorator(func) 
func_obj()

Output

  • This is inside function before execution
  • I am argument function

answered Jan 2, 2020 at 12:19

Python input function in function

Usman GaniUsman Gani

3193 silver badges8 bronze badges

1

  1. Yes, it's allowed.
  2. You use the function as you would any other: anotherfunc(*extraArgs)

answered Jun 9, 2011 at 7:49

sepp2ksepp2k

357k52 gold badges667 silver badges670 bronze badges

  1. Yes. By including the function call in your input argument/s, you can call two (or more) functions at once.

For example:

def anotherfunc(inputarg1, inputarg2):
    pass
def myfunc(func = anotherfunc):
    print func

When you call myfunc, you do this:

myfunc(anotherfunc(inputarg1, inputarg2))

This will print the return value of anotherfunc.

Hope this helps!

answered May 17, 2014 at 7:02

Python input function in function

ArialArial

4,4662 gold badges17 silver badges17 bronze badges

def x(a):
    print(a)
    return a

def y(func_to_run, a):
    return func_to_run(a)

y(x, 1)

That I think would be a more proper sample. Now what I wonder is if there is a way to code the function to use within the argument submission to another function. I believe there is in C++, but in Python I am not sure.

answered Jul 26, 2018 at 14:33

Python input function in function

def x(a):
    print(a)
    return a

def y(a):
    return a

y(x(1))

answered Apr 29, 2018 at 11:34

2

Can we take input in function in Python?

In Python, we use input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function convert it into a string.

How do you pass an input to a function in Python?

Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

Does input () function return string?

By default, input returns a string. So the name and age will be stored as strings.

What is the function of input () function in Python give suitable example?

Python input() function is used to get input from the user. It prompts for the user input and reads a line. After reading data, it converts it into a string and returns that. It throws an error EOFError if EOF is read.