Guy Adini Guy Adini - 1 year ago 90
Python Question

Python - test whether object is a builtin function

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?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download