meyer_mit_ai
meyer_mit_ai

Reputation: 35

Is it possible to use instantiation with quotes for a custom str class?

I wrote a custom str class, because I want to have a casefold() method that replaces German and Scandinavian umlauts (ä, å, ö, ø, ü…). It inherits from the standard str class and just supersedes the casefold() method after making use of it.

class str(str):
    def casefold(self):
        umlauts: dict ={
            'ä': 'ae',
            'ö': 'oe',
            'ü': 'ue',
            'œ': 'oe',
            'ø': 'oe',
            'æ': 'ae',
            'å': 'aa',
        }
        string_ = self.__repr__().casefold()
        for letter in string_:
            if letter in umlauts:
                string_ = string_.replace(letter, umlauts[letter])
        return string_

if I use str() to instantiate a string, it works fine:

str('äåöøü').casefold()
"'aeaaoeoeue'"

But when I instantiate with '', the standard str class is used.

'äåöøüß'.casefold()
'äåöøüss'

type(str('äåöøü'))
<class '__main__.str'>

type('äåöøüß')
<class 'str'>

Is there a way to change this, to change the instantiation with '' to the custom class? I couldn’t find anything on this.

Upvotes: 1

Views: 79

Answers (1)

jsbueno
jsbueno

Reputation: 110086

That is not possible in current Python (up to 3.13) - the language literals will only create instances of the standard built-in classes.

Though fiddling with the import machinery and the AST, it actually would be possible to do something like that - but it is not the norm, it would be a complicated code, and above all, one person looking at your source file containing the to-be-custom instantiated literal strings would not KNOW that (but for comments hanging around). The behavior would depend on code installing a custom importer being run previous to any source file containing the custom strings -

TL;DR: although feasible, it is not a thing I would recomend - rather, you can have a class you import with a single-letter alias, and just add a L('') around your string literals. (I usually do that for multi-line strings I want to dedent, for example from textwrap import dedent as D.

All that said, keep an eye on PEP 750 - Template Strings - It is under active discussion, and may emerge as a powerful new feature for the language in the upcoming years. If approved, it would allow custom prefixes to the quote-characters, which them could instantiate your str subclass, and add even more flexibility.

Discussion on the PEP is active here - https://discuss.python.org/t/pep-750-tag-strings-for-writing-domain-specific-languages/60408/39

Upvotes: 3

Related Questions