Python - test whether object is a builtin function

后端 未结 5 896
面向向阳花
面向向阳花 2020-12-19 06:16

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)

beca

相关标签:
5条回答
  • 2020-12-19 06:37

    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()
    
    0 讨论(0)
  • 2020-12-19 06:43

    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:
    1. function
    2. 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.

    0 讨论(0)
  • 2020-12-19 06:48

    Try this:

    >>> import types
    >>> isinstance(pow, types.BuiltinFunctionType)
    True
    >>> def a():
        pass
    >>> isinstance(a, types.BuiltinFunctionType)
    False
    
    0 讨论(0)
  • 2020-12-19 06:59

    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)
    
    0 讨论(0)
  • 2020-12-19 07:02

    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
    >>>
    
    0 讨论(0)
提交回复
热议问题