I want to define a new class that inherit the build in str type, and create a method that duplicates the string contents.
How do I get access to the string value assigned to the object of my new class ?
class str_usr(str):
def __new__(cls, arg):
return str.__new__(cls, arg)
def dub(self):
# How to modify the string value in self ?
self.<attr> = self.<attr> + self.<attr>
Thanks for any help 🙂
Strings in Python are immutable, so once you have one string, you can’t change its value. It’s almost the same as if you had a class derived from
int, and then you added a method to change the value of theint.You can of course return a new value: