Reputation: 5494
Is there a nice way to check whether object o is a builtin Python function?
I know I can use, for example
type(o) == type(pow)
because type(pow) is 'builtin_function_or_method'.
But is there some nicer way?
Upvotes: 14
Views: 6778
Reputation: 623
You can check if builtins has it as an attribute and it's callable. Samples:
>>> import builtins
>>> hasattr(builtins, 'max')
True
>>> hasattr(builtins, 'abs')
True
>>> hasattr(builtins, 'aaa')
False
>>> hasattr(builtins, 'True')
True
>>> callable(abs)
True
>>> callable(True)
False
>>> hasattr(builtins, 'max') and callable(max)
True
>>> hasattr(builtins, 'True') and callable(True)
False
Upvotes: 1
Reputation: 75
Although some time has passed, perhaps other people will find my answer useful since this question has popped up in my search for an answer.
In case you are only interested in testing rather a given variable var
is one of the following:
function
builtin_function_or_method
but not a method
I found the following test to work:
import types
def is_func(arg):
return isinstance(arg, types.FunctionType) or \
(isinstance(arg, types.BuiltinFunctionType) and
arg.__self__ is None)
Hope i am correct in my assumption, but the general idea is that if we have been given a builtin_method
it must have a type not None
associated with it.
Upvotes: 1
Reputation: 29397
It depends what you mean by “built-in”.
Using __builtins__
If you want to check that your function is one of the built-in functions in the Python interpreter you can use
>>> pow in __builtins__.__dict__.values()
True
>>> __builtins__.__dict__['pow']
<built-in function pow>
The Python interpreter has a number of built-in constants, functions, types, and exceptions, that are contained in the dictionary __builtins__.__dict__
.
Using BuiltinFunctionType
If on the other hand you want to check if your function is of type BuiltinFunctionType
you can use the types
module
>>> import types
>>> isinstance(pow, types.BuiltinFunctionType)
True
Using inspect
Or inspect.isbuiltin
(just a wrapper around isinstance(object, types.BuiltinFunctionType)
)
>>> import inspect
>>> inspect.isbuiltin(pow)
True
Note that the term “built-in” in BuiltinFunctionType
means “written in C”.
Consider the following example:
>>> from math import factorial
>>> isinstance(factorial, types.BuiltinFunctionType)
True
The factorial
function is of type BuiltinFunctionType
but it's not a builtin function in the interpreter
>>> factorial in __builtins__.__dict__.values()
False
This is because the math
module in Python consists of wrappers around the C math library functions.
Being able to detect a BuiltinFunctionType
is useful because for functions written in Python one can inspect the source code without having to open the source files.
>>> import random
>>> isinstance(random.random, types.BuiltinFunctionType)
True
>>> inspect.getsource(random.random)
# returns TypeError
>>> isinstance(random.uniform, types.BuiltinFunctionType)
False
>>> from __future__ import print_function # if using Python 2.*
>>> print(inspect.getsource(random.uniform))
def uniform(self, a, b):
"Get a random number in the range [a, b) or [a, b] depending on rounding."
return a + (b-a) * self.random()
Upvotes: 6
Reputation: 36549
The types module:
>>> import types
>>> types.BuiltinFunctionType
<type 'builtin_function_or_method'>
Though, if you look under the hood, you'll find it's not that different from what you're doing now.
So, in your case, use
isinstance(o, types.BuiltinFunctionType)
Upvotes: 20
Reputation: 101122
you can also do
import __builtin__
o in __builtin__.__dict__.values()
or, in CPython:
o in __builtins__.__dict__.values()
but note that you rely on an implementation detail here.
>>> pow in __builtins__.__dict__.values()
True
>>> def a():
... pass
...
>>> a in __builtins__.__dict__.values()
False
>>>
Upvotes: 2
Reputation: 12931
Try this:
>>> import types
>>> isinstance(pow, types.BuiltinFunctionType)
True
>>> def a():
pass
>>> isinstance(a, types.BuiltinFunctionType)
False
Upvotes: 3