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'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
You can use types.FunctionType
:
>>> def x():
... pass
...
>>> import types
>>> isinstance(x, types.FunctionType)
True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With