List of built-in functions and methods (Python)

I came up with this:

[a for a in dir(__builtins__) if str(type(getattr(__builtins__,a))) == "<type 'builtin_function_or_method'>"]

I know its ugly. Can you show me a better / more pythonic way to do this?

+3
source share
2 answers

There is a inspectmodule :

import inspect

filter(inspect.isbuiltin, (member for name, member in inspect.getmembers(__builtins__)))

Edit: After reading the documentation a bit carefully, I came up with this option that doesn't use __getattr__

import inspect

members = (member for name, member in inspect.getmembers(__builtins__))
filter(inspect.isbuiltin, members)
+6
source

Here is a variation without getattr:

import inspect
[n.__name__ for n in __builtins__.__dict__.values() if inspect.isbuiltin(n)]

And if you want to use the actual function pointers:

import inspect
[n for n in __builtins__.__dict__.values() if inspect.isbuiltin(n)]
+2
source

Source: https://habr.com/ru/post/1713057/


All Articles