[Python-Dev] Adding bytes.frombuffer() constructor to PEP 467 (was: [Python-ideas] Adding bytes.frombuffer() constructor

Nick Coghlan ncoghlan at gmail.com
Wed Oct 12 23:31:02 EDT 2016


On 13 October 2016 at 12:54, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Method proliferation on builtins is a Big Deal(TM)

I wanted to quantify this concept, so here's a quick metric that helps
convey how every time we add a new builtin method we're immediately
making Python harder to comprehend:

    >>> def get_builtin_types():
    ...     import builtins
    ...     return {name:obj for name, obj in vars(builtins).items()
if isinstance(obj, type) and not (name.startswith("__") or
issubclass(obj, BaseException))}
    ...
    >>> len(get_builtin_types())
    26
    >>> def get_builtin_methods():
    ...     return [(name, method_name) for name, obj in
get_builtin_types().items() for method_name, method in
vars(obj).items() if not method_name.startswith("__")]
    ...
    >>> len(get_builtin_methods())
    230

Putting special purpose functionality behind an import gate helps to
provide a more explicit context of use (in this case, IO buffer
manipulation) vs the relatively domain independent namespace that is
the builtins.

Cheers,
Nick.

P.S. Since I was poking around in the builtins anyway, here are some
other simple language complexity metrics:

    >>> len(vars(builtins))
    151
    >>> def get_interpreter_builtins():
    ...     import builtins
    ...     return {name:obj for name, obj in vars(builtins).items()
if name.startswith("__")}
    ...
    >>> len(get_interpreter_builtins())
    8
    >>> def get_builtin_exceptions():
    ...     import builtins
    ...     return {name:obj for name, obj in vars(builtins).items()
if isinstance(obj, type) and issubclass(obj, BaseException)}
    ...
    >>> len(get_builtin_exceptions())
    65
    >>> def get_builtin_functions():
    ...     import builtins
    ...     return {name:obj for name, obj in vars(builtins).items()
if isinstance(obj, type(repr))}
    ...
    >>> len(get_builtin_functions())
    42
    >>> def get_other_builtins():
    ...     import builtins
    ...     return {name:obj for name, obj in vars(builtins).items()
if not name.startswith("__") and not isinstance(obj, (type,
type(repr)))}
    ...
    >>> len(get_other_builtins())
    12

The "other" builtins are the builtin constants (None, True, False,
Ellipsis, NotImplemented) and various artifacts from doing this at the
interactive prompt (license, credits, copyright, quit, exit, help,
"_")

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list