0
def example(function):
    if input() == "Hello there!":
        #at this point I want to call the function entered in the tuples

an example of what I mean:

def example(function):
    if input() == "Hello there!":
        #do the function here

def Printer(What_to_print):
    print(What_to_print + "Just an example")


example(Printer)

Is this possibe and are there drawbacks in doing this?

Andreas Sandberg
  • 241
  • 1
  • 3
  • 10
  • Do you mean passing function as an argument to another function? Yes, it is possible and is quite common. – bereal Aug 13 '16 at 18:42
  • Yes it's entirely possible. – Harrison Aug 13 '16 at 18:44
  • `at this point I want to call the function` - So do that? – Stefan Pochmann Aug 13 '16 at 18:55
  • It isn't just possible. [Several](https://docs.python.org/3/library/functions.html#map) built-in [functions](https://docs.python.org/3/library/functions.html#filter) take [functions](https://docs.python.org/3/library/functions.html#classmethod) as [arguments](https://docs.python.org/3/library/functions.html#staticmethod). – zondo Aug 13 '16 at 19:36
  • Those things the function's `function` argument is between are called [parentheses](http://www.dictionary.com/browse/parentheses). A [`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple) is a built-in Python sequence type whose construction can look very similar. – martineau Aug 13 '16 at 20:15

3 Answers3

0

In python, functions are objects like any other common types, like ints and strs. Therefore, there is no problem with a function that receives another function as an argument.

>>> def pr(): print ('yay')

>>> def func(f): f()

>>> isinstance(pr, object)
True
>>> isinstance(int, object)
True
>>> func(pr)
yay
>>> 
kmaork
  • 5,722
  • 2
  • 23
  • 40
0

Yes. It is possible.

def example(function):
    if input() == "Hello there!":
        function("Hello there!")  # invoke it!

Actually you can pass def functions and lambda functions as parameters and invoke them by () syntax.

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • Sorry for asking this (i'm really new to coding) but could you give me an example of what you mean by invoking it? – Andreas Sandberg Aug 13 '16 at 19:20
  • @AndreasSandberg By invoking, I mean calling the function. In your words, _doing_ the function. – frogatto Aug 13 '16 at 19:20
  • I probaly sound really stupid right now, but I don't know what to add after the ; and when I remove it the `example` prints what I want it to print without me doing anything, and when I type in Hello there! I just get lots of errors. – Andreas Sandberg Aug 13 '16 at 19:43
  • @AndreasSandberg Sorry, my bad! Python doesn't need semicolons. updated my answer. – frogatto Aug 13 '16 at 19:45
  • this when ran just calls the function in it without me having to type Hello there! and when I type Hello there! it says TypeError: 'NoneType' object is not callable – Andreas Sandberg Aug 13 '16 at 19:51
  • @AndreasSandberg updated my answer again. You can simply pass all needed parameters to the function being called as it would in regular functions calls. – frogatto Aug 13 '16 at 19:59
0
def example(function, what_to_print):
    if raw_input() == "Hello there!":
        function(what_to_print)

def printer(what_to_print):
    print(what_to_print + "Just an example")

example(printer, "")
cridnirk
  • 308
  • 4
  • 15