5

I'm trying to put together a canonical example of how to get a list of all the builtin functions in Python. The documentation is good, but I want to demonstrate it with a provable approach.

Here, I'm essentially defining the built-in functions as the members of the default namespace that are usable and consistent with the stylistic characteristics of a function that is intended for use in a module, that is: they provide some useful functionality and begin with a lowercase letter of the alphabet.

The upside of what I'm doing here is that I'm demonstrating the filter part of list comprehensions, but it seems a bit of a dirty hack and like there should be a more straight-forward way of doing this. Here's what I'm doing so far:

import string
alc = string.ascii_lowercase
bif = [i for i in dir(__builtins__) if 
       any(i.startswith(j) for j in alc)]

Which gives me:

['abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']

I believe that they are all callable, as with this:

bi2 = [i for i in dir(__builtins__) if 
       any(i.startswith(j) for j in alc) 
       and callable(getattr(__builtins__, i, None))]
set(bif).symmetric_difference(bi2)

I get:

set([])

So is there a better way to list the builtin Python functions? Google and stackoverflow searches have failed me so far.

I am looking for a demonstrable and repeatable method for experimental instruction. Thanks!

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • 1
    Why do you need to list them in Python instead of just looking in [the documentation](http://docs.python.org/2/library/functions.html)? – BrenBarn Dec 18 '13 at 06:42
  • 1
    I'm demonstrating a canonical approach to answering the question of how many built-in functions are there and how can I track them to determine if I know them all. – Russia Must Remove Putin Dec 18 '13 at 06:44
  • @AaronHall: That sounds like you want the list in the documentation, then. It's a human task, not a coding task. – user2357112 Dec 18 '13 at 06:45

1 Answers1

7
import __builtin__
import inspect

[name for name, function in sorted(vars(__builtin__).items())
 if inspect.isbuiltin(function) or inspect.isfunction(function)]

There's also the list in the documentation.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Discrepancy `>>> __builtin__ = __builtins__ >>> bi4 = [name for name, function in sorted(vars(__builtin__).items()) ... if inspect.isbuiltin(function) or inspect.isfunction(function)] >>> set(bif).symmetric_difference(bi4) set(['bytearray', 'enumerate', 'set', 'help', 'int', '__import__', 'float', 'unicode', 'memoryview', 'bool', 'quit', 'basestring', 'slice', 'complex', 'long', 'xrange', 'tuple', 'exit', 'type', 'file', 'bytes', 'buffer', 'reversed', 'object', 'dict', 'credits', 'frozenset', 'super', 'copyright', 'license', 'classmethod', 'list', 'staticmethod', 'str', 'property'])` – Russia Must Remove Putin Dec 18 '13 at 06:50
  • @AaronHall: Some of those are types. `list` and `int`, for example. Others are things like the copyright notice. `__import__` appears in my list but not yours; it's a built-in function. If you want everything callable, that's a simple change, though it'd put the exceptions back in the list. – user2357112 Dec 18 '13 at 06:53
  • Well, +1 for the effort, but while my list misses dunder import (which interestingly enough, I've recently used), it gets all the rest of the callable items in the namespace that people should be aware of when using Python. The point for me is to be instructive to new users of Python. – Russia Must Remove Putin Dec 18 '13 at 06:57
  • 1
    @AaronHall: If you want instructive, you want the documentation. It's much easier than trying to find a formula for "instructive to new users", and it's more helpful and more readable than a list of names. – user2357112 Dec 18 '13 at 07:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43396/discussion-between-aaron-hall-and-user2357112) – Russia Must Remove Putin Dec 18 '13 at 07:08