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 ?

0

2 Answers 2

10

You can use types.FunctionType:

>>> def x():
...     pass
...
>>> import types
>>> isinstance(x, types.FunctionType)
True
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.