1

I've made a function that takes a function as an argument.

If I want to specify that a function takes an integer it's pretty easy:

def fun(myInt:int=0):
    return myInt

But when it takes a function I can't do this:

def fun(myFun:function=print):
    myFun('whatever')

I printed type() of both an integer and a function and got <class 'int'> <class 'function'>

What is the difference between these two and is an easy way to do what I wanted to do?

Btw. I've already used fellow google and couldn't find anything

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

1

In Python 3:

from typing import Callable

def fun(myFun:Callable=print):
    myFun('whatever')

fun()
fun(print)
fun(type)