Home > OS >  Nested function in Python class
Nested function in Python class

Time:04-01

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

CodePudding user response:

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.

CodePudding user response:

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.

  • Related