0

i have a little "basic understanding" Python problem. So let me explain my problem.
At first a very simple code snippet.

class Revert:
    __sentence = ""

    def __init__(self, sentence: str):
        self.__sentence = sentence

    def get_sentence(self):
        return self.__sentence

    def revert_sentence(self):
        return self.__sentence[::-1]


if __name__ == '__main__':
    print(Revert("Stackoverflow").get_sentence())
    print(Revert("Stackoverflow").revert_sentence())

So this show normal function calling of python functions. But how can i transform this code so i can call the revert function like this:

    print(Revert("Stackoverflow").get_sentence().revert_sentence())

Maybe I'm miss the forest through the trees. But I didn't get it how to do this. I already tried to solve the problem with innermethods but this didn't work for me

...
    def get_sentence(self):
        def revert_sentence():
            self.revert_sentence()

        return self.__sentence
...

Many thanks in advance

1
  • You must understand what an object of a class is. And how you call a function on an object instance. If you want to chain the functions you must return the Object from the first function not a member of the Object(or class). Commented Mar 31, 2022 at 16:36

2 Answers 2

2

Implement __str__ to return the actual string. Then in the existing methods, return the object. This way you can chain. But when print is applied to it, that __str__ method will kick in:

class Revert:
    __sentence = ""

    def __init__(self, sentence: str):
        self.__sentence = sentence

    def get_sentence(self):
        return self

    def revert_sentence(self):
        return Revert(self.__sentence[::-1])

    # Some more such methods ...
    def upper(self):
        return Revert(self.__sentence.upper())

    def first(self, count):
        return Revert(self.__sentence[:count])

    def dotted(self):
        return Revert(".".join(self.__sentence))

    # For getting a string
    def __str__(self):
        return self.__sentence


print(Revert("Stackoverflow").get_sentence().revert_sentence())

print(Revert("Stackoverflow")
         .revert_sentence()
         .first(8)
         .upper()
         .revert_sentence()
         .first(4)
         .dotted())   # "O.V.E.R"

Note that now the .get_sentence() method is not really doing much, and you can always strip it from a chain.

Sign up to request clarification or add additional context in comments.

3 Comments

This is more a comment for Mo7art than for you, but it doesn't really make sense to have a member function that just returns the object. You already need to have the object before you call .get_sentence(). Maybe this is a simplified example for Stack Overflow, and the actual .get_sentence() actually operates on the self object and returns the result, which would make more sense.
@PranavHosangadi Yes this was a very simplified code sample, in the original code I do something in the here called .get_sentence() function. Also, there should be many of the kind of functions like get_sentence() would that work there too ? @trincot The problem is, that I didn't call the print method (only here in the example). I would like to work with the returned Object in other functions.
Sure, more functions would work too. I added a few more to my answer.
1

Here You go:

class Revert:
    __sentence = ""

    def __init__(self, sentence: str):
        self.__sentence = sentence

    def get_sentence(self):
        return self.__sentence

    def revert_sentence(self):
        # It's important to know that you are making changes in the same instance of the object
        self.__sentence = self.__sentence[::-1]
        return self
    
    def pseudo_revert(self):
        # Return a new object with reverted string, but this instance still has original string intact.
        return Revert(self.__sentence[::-1])


if __name__ == '__main__':
    r1 = Revert("Stackoverflow")
    r2 = Revert("Stackoverflow")

    print(r1.get_sentence()) # Stackoverflow
    print(r1.revert_sentence().get_sentence()) # wolfrevokcatS
    print(r1.get_sentence()) # wolfrevokcatS
    
    print(r2.get_sentence()) # Stackoverflow
    print(r2.pseudo_revert().get_sentence()) # wolfrevokcatS
    print(r2.get_sentence()) # Stackoverflow
    

Hope this helps you understand the object, instance of an object, and method of object distinctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.