[Python-ideas] More alternate constructors for builtin type

Ricky Teachey ricky at teachey.org
Tue May 7 14:43:15 EDT 2019


One pain point in Python that constantly rears its head (at least for me)
is this:

def eat_iterable(yummy_iterable):
    if isinstance(yummy_iterable, str):
        raise TypeError("BLECH! Strings are ATOMIC in this context,
mmkay??")
    tasty_list = list(yummy_iterable)
    # digest list...

The design decision to make strs sequence-like was definitely a good one
with a huge number of advantages. But we all know that sometimes, treating
strs just like all other kinds of sequences is very inconvenient and isn't
really what we want.

This leads to isinstance() checking, which feels very non-python every time
I do it. It would be kind of nice if list, tuple, and set had alt
constructors that disallowed things that aren't REALLY sequences of
individual atomic items-- like strs, and perhaps dicts as well (which is
another instance check often needed to guard against errors).

Perhaps:

list.atomics
tuple.atomics
set.atomics

But naming things is hard and I'm terrible at it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190507/c5ce7522/attachment.html>


More information about the Python-ideas mailing list