Michael Clerx
Michael Clerx

Reputation: 3056

Get a list of python's essential functions

For syntax highlighting of Python in Python, I'm using the "keywords" module to get a list of the keywords (for, in, raise etc.).

But how can I get a list of essential built-in functions? I.E. the ones listed here: https://docs.python.org/2/library/functions.html

(I want to do it programmatically of course, in case the list ever changes)

Upvotes: 2

Views: 168

Answers (2)

vaultah
vaultah

Reputation: 46533

dir(builtins) is not enough, simply because builtins module also exposes exceptions and warnings, as well as False, True, None and lots of other constants and "internal" functions.

You could test the type of the object

import builtins # __builtin__ in Python 2
from inspect import isbuiltin

for name, val in vars(builtins).items():
    if isbuiltin(val):
        print(name)

but even then, in Python 3 the output would include __build_class__ which isn't in the list of Built-in Functions.

Really, it's fine to hardcode names of built-in functions.

Upvotes: 4

Ulisha
Ulisha

Reputation: 154

You can get a list of the builtins functions in python typing the following:

print dir(__builtins__)

Upvotes: 0

Related Questions