Python Forum
How to retrieve all and only built-in functions from interactive interpreter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to retrieve all and only built-in functions from interactive interpreter
#1
Python documentation lists 69 built-in functions (compared to 3.6 there is one new built-in function in 3.7 (breakpoint()).

How do I get this exact same list of built-in functions in interactive interpreter?

One can try __builtins__:

>>> dir(__builtins__)[80:]
['abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes',
'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 
'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 
'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 
'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 
'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open',
'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set',
'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 
'vars', 'zip']
This is very close, however it will give 'copyright', 'credits', 'license' which are not listed in documentation and misses __import__().

One can try filter __builtin__.__dict__ values:

>>> for key, value in __builtins__.__dict__.items():
...     if 'built-in function' in repr(value):
...         print(key, end=',')
...
__build_class__,__import__,abs,all,any,ascii,bin,breakpoint,callable,chr,
compile,delattr,dir,divmod,eval,exec,format,getattr,globals,hasattr,hash,
hex,id,input,isinstance,issubclass,iter,len,locals,max,min,next,oct,ord,pow,
print,repr,round,setattr,sorted,sum,vars,open,>>> 
This misses several built-in functions which has values as class (for example: ''memoryview': <class 'memoryview'>, 'bytearray': <class 'bytearray'>, 'bytes': <class 'bytes'>, 'classmethod': <class 'classmethod'>, 'complex': <class 'complex'> etc). Modifying condition to include keys which values contain 'class' will bring also all errors, KeyboardInterrupt etc.

There is types module, but in types.BuiltinFunctionType the term “built-in” means “written in C”

There is inspect module and inspect.isbuiltin(object) which will return true if the object is a built-in function or a bound built-in method. However, it will give same result as with builtins dict:

>>> import inspect
>>> for item in dir(__builtins__):
...     if inspect.isbuiltin(eval(item)):
...         print(item, end=',')
...
__build_class__,__import__,abs,all,any,ascii,bin,breakpoint,callable,chr,
compile,delattr,dir,divmod,eval,exec,format,getattr,globals,hasattr,hash,
hex,id,input,isinstance,issubclass,iter,len,locals,max,min,next,oct,open,ord,pow,
print,repr,round,setattr,sorted,sum,vars,>>>
So - is there pythonic way do get all and only 69 functions listed in documentation from interactive interprpeter?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#2
just an fyi
there's a package that lists all libraries named stdlib_list, located here: https://github.com/jackmaneyof
it can be installed with pip
docs are here: http://python-stdlib-list.readthedocs.io/en/latest/
It might be interesting to examine the code.
Reply
#3
(Aug-07-2018, 08:50 PM)perfringo Wrote: So - is there pythonic way do get all and only 69 functions listed in documentation from interactive interprpeter?
I don't know if this is pythonic, but you can do
url = "https://github.com/python/cpython/raw/3.7/Doc/library/functions.rst"
from urllib.request import urlopen
data = urlopen(url).read()
then parse the data conveniently to get the list.
Reply
#4
Thank you for your efforts Larz60+ and Gribouillis! Especially I like Gribouillis workaround Smile
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Calling out to network resources is not always possible.
The task should be solvable without an internet connection.
I guess it's more handcrafting to sort things out.


from types import BuiltinFunctionType, BuiltinMethodType
import builtins
import sys
from itertools import zip_longest

allowed_types = (BuiltinFunctionType, BuiltinMethodType) # maybe you can use this later
b_list = sorted([b for b in dir(builtins) if b.islower() and not b.startswith('_')])
b_list += ['__import__']

def print_table(iterable, rows=14):
    chunked = zip_longest(*[iter(iterable)] * rows, fillvalue='')
    transposed = zip(*chunked)
    for row in transposed:
        for col in row:
            print(f'{col:<20}', end='')
        else:
            print('', sep='')


print(sys.version)
print_table(b_list)
Output:
deadeye@nexus ~ $ python builtins_printer.py 3.7.0 (default, Aug 3 2018, 02:17:20) [GCC 8.1.1 20180531] abs copyright getattr list quit type all credits globals locals range vars any delattr hasattr map repr zip ascii dict hash max reversed __import__ bin dir help memoryview round bool divmod hex min set breakpoint enumerate id next setattr bytearray eval input object slice bytes exec int oct sorted callable exit isinstance open staticmethod chr filter issubclass ord str classmethod float iter pow sum compile format len print super complex frozenset license property tuple
For example credits and copyright are not listed as bultins in the documentation.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interactive plots that can be embed in web page mouse9095 1 595 Jun-12-2023, 04:51 PM
Last Post: deanhystad
  Why built in functions are defined as class? quazirfan 5 2,775 Oct-23-2021, 01:20 PM
Last Post: Gribouillis
  interactive map and function hurc60248 1 1,752 Jan-08-2021, 09:22 PM
Last Post: Larz60+
  Question about functions(built-in) Jinja2Exploitation 2 1,932 Nov-15-2020, 02:13 PM
Last Post: jefsummers
  Interactive Menu, String Search? maggotspawn 3 2,579 May-11-2020, 05:25 PM
Last Post: menator01
  for / else not working in interactive mode Skaperen 4 2,556 Jul-17-2019, 06:16 PM
Last Post: Skaperen
  How to recognize, what functions are filled in of the Python interpreter stack? AlekseyPython 3 2,681 Mar-13-2019, 12:14 PM
Last Post: AlekseyPython
  Problem with interactive python prompt Groot_04 4 3,130 Aug-02-2018, 08:39 AM
Last Post: Groot_04
  tale interactive fiction - looking for tutorial aryell8linx 1 2,643 May-29-2018, 12:41 PM
Last Post: Larz60+
  Built in Functions not working mhoneycutt83 4 6,345 Jul-28-2017, 07:39 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020