On Tue, Dec 20, 2022 at 6:20 PM Lucas Wiman <lucas.wi...@gmail.com> wrote:
> On Tue, Dec 20, 2022 at 5:38 PM Christopher Barker <python...@gmail.com> > wrote: > >> But collections.UserString does exist -- so if you want to subclass, and >> performance isn't critical, then use that. Steven A pointed out that >> UserStrings are not instances of str though. I think THAT is a bug. And >> it's probably that way because with the magic of duck typing, no one cared >> -- but with all the static type hinting going on now, that is a bigger >> liability than it used to be. Also basue when it was written, you couldn't >> subclass str. >> >> Though I will note that run-time type checking of string is relatively >> common compared to other types, due to the whole a-str-is-a-sequence-of-str >> issue making the distinction between a sequence of strings and a string >> itself is sometimes needed. And str is rarely duck typed. >> > > Note that UserString does break some built-in functionality, like you > can't apply regular expressions to a UserString: > >>> class FooString(UserString): > ... pass > ... > >>> re.compile(r"asdf").match(FooString("asdf")) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: expected string or bytes-like object, got 'FooString' > > > There is more discussion in this thread ( > https://stackoverflow.com/questions/59756050/python3-when-userstring-does-not-behave-as-a-string), > including a link to a very old bug (https://bugs.python.org/issue232493). > I wonder how many of these issues would go away if userString subclassed for str. Maybe some? But at the C level, duck typing simply doesn't work -- you need access to an actual C string struct. Code that worked with strings *could* have a little bit of wrapper for subclasses that would dig into it to find the actual str underneath -- but if that code had to be written everywhere strings are used in C -- that could be a pretty big project -- probably what Guido meant by: "Fixing this will be a major project, probably for Python 3000k" I don't suppose it has been addressed at all? Note: at least for string paths, the builtins all use fspath() (or something) so that should be easy to make work. (and seems to with my prototype already) There is a related issue with json.dump etc, json.dump works with my prototype as well. -CHB -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/62JRML5OPTLW653RSN6GZD4ZE3TCZ572/ Code of Conduct: http://python.org/psf/codeofconduct/