DEV Community

Discussion on: Share a code snippet in any programming language and describe what's going on

Collapse
 
arjunsahlot profile image
Arjun

Python:

class str(str):
    def like_a_cow(self):
        return f"Moo {self} mooooo"

str("Hello").like_a_cow()
> "Moo Hello mooooo"
Enter fullscreen mode Exit fullscreen mode

I should add that this is not good practice. str is a built in function and you should not override it.