-1
"this is my string".myfunction(argument)

This is very simple in javascript. With the keyword this i can access to my string directly. Is that possible with python?

0

1 Answer 1

1

You can inherit from str and define your own methods:

class myString(str):
    def my_method(self, ...):
        # ...

some_string = myString("StackOverflow")

print some_string.count("a") # method from string
print some_string.myMethod(...) # your defined method
Sign up to request clarification or add additional context in comments.

13 Comments

But i wouldn't like to declare each string as instance of a class. Does not exists more ways to do this?
Maybe you can just create a function that receives two parameters: my_funcion(str1, str2) It will be "similar" to str1.my_function(str2)
Yeah i think that is the only way to do it. But i wanted to extend the function join. Something like ",".join_reverse(list) but is not possible with this language.
You can always use join() in the new function you will create. I don't see the point of "extending" it.
Can i do this: "my spliter".join_reverse(list) instead of join_reverse("my spliter", list) ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.