Hi!

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)
        else:
            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.
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to