11

in python 3 every things are objs , functions too. functions are first-class citizens that mean we can do like other variables.

>>> class x:
    pass

>>> 
>>> isinstance(x,type)
True
>>> type(x)
<class 'type'>
>>> 
>>> x=12
>>> isinstance(x,int)
True
>>> type(x)
<class 'int'>
>>> 

but functions are diffrent ! :

>>> def x():
    pass

>>> type(x)
<class 'function'>
>>> isinstance(x,function)
Traceback (most recent call last):
  File "<pyshell#56>", line 1, in <module>
    isinstance(x,function)
NameError: name 'function' is not defined
>>>

why error ? what is python functions type ?

falsetru
  • 357,413
  • 63
  • 732
  • 636
Zero Days
  • 851
  • 1
  • 11
  • 22

2 Answers2

10

You can use types.FunctionType:

>>> def x():
...     pass
...
>>> import types
>>> isinstance(x, types.FunctionType)
True
Dan D.
  • 73,243
  • 15
  • 104
  • 123
falsetru
  • 357,413
  • 63
  • 732
  • 636
5

@falsetru's answer is correct for function's type.

But if what you are looking for is to check whether a particular object can be called using () , then you can use the built-in function callable(). Example -

>>> def f():
...  pass
...
>>> class CA:
...     pass
...
>>> callable(f)
True
>>> callable(CA)
True
>>> callable(int)
True
>>> a = 1
>>> callable(a)
False
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176