[Python-ideas] Split, slice, join and return "syntax" for str

Andrés Delfino adelfino at gmail.com
Sun Mar 4 12:59:40 EST 2018


I was thinking: perhaps it would be nice to be able to quicky split a
string, do some slicing, and then obtaining the joined string back.

Say we have the string: "docs.python.org", and we want to change "docs" to
"wiki". Of course, there are a ton of simpler ways to solve this particular
need, but perhaps str could have something like this:

spam = "docs.python.org"
eggs = "wiki." + spam['.'][1:]
print(eggs) #wiki.python.org

A quick implementation to get the idea and try it:

class Mystr(str):
    def __getitem__(self, item):
        if isinstance(item, str):
            return Mystr_helper(self, item)
            return super().__getitem__(item)

class Mystr_helper:
    def __init__(self, obj, sep):
        self.obj = obj
        self.sep = sep
    def __getitem__(self, item):
        return self.sep.join(self.obj.split(self.sep)[item])

What are your thoughts?

Greetings from Argentina.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180304/62e4b936/attachment.html>

More information about the Python-ideas mailing list