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?
Python isinstance() Function The isinstance() function returns True if the specified object is of the specified type, otherwise False . If the type parameter is a tuple, this function will return True if the object is one of the types in the tuple.
The isinstance() function checks if the object (first argument) is an instance or subclass of classinfo class (second argument).
To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.
Using isinstance() function, we can test whether an object/variable is an instance of the specified type or class such as int or list. In the case of inheritance, we can checks if the specified class is the parent class of an object. For example, isinstance(x, int) to check if x is an instance of a class int .
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
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