On Tue, May 7, 2019 at 11:43 AM Ricky Teachey
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.
I've always thought this was a wart in Python, and the one type error that does show up a lot. (with true division now, it's the only type error that shows up a lot...) But the "problem" isn't that strings are a sequence -- as you say that is handy. The problem is that there is now character type -- so a string is not a sequence of characters, it's a sequence of length-1 strings -- which leads to potentially infinite recursion as you drill down to get the single item. But introducing a character type has been rejected multiple times. Oh well.
This leads to isinstance() checking, which feels very non-python every time I do it.
Agreed. And it's only an adequate solution because for all intents and purposes, there is only one string type -- it would be very rare to duck type a string.
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
yeach! as you said, a string is a sequence, and that's a good thing. and dicts being an iterable of the keys is kind of nice, too -- and once you have that, they they should be able to be passed into into sequence constructors. (and I actually have done list(a_dict) on purpose when I needed a list of the keys in a dict. -- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython