Python function return value on call and nested function

134 views Asked by At

I have a comprehension problem in python, maybe there is a simple solution for it, or maybe a design pattern.

class SampleClass:
    __dictionary = dict()

    def __init__(self, sentence: dict):
        self.__dictionary = sentence

    def get_a(self):
        def transform_to_another_object():
            # call some other methods which return a new object 
        return self.__dictionary["a"]

    def get_b(self):
        def transform_to_another_object():
            # call some other methods which return a new object 
        return self.__dictionary["b"]

    def get_c(self):
        def transform_to_another_object():
            # call some other methods which return a new object 
        return self.__dictionary["c"]

if __name__ == '__main__':
    example = {"a": {"SampleObjectA"}, "b": {"SampleObjectA"}, "c": {"SampleObjectA"}}
    sample = SampleClass(example)
    new_object_a = sample.get_a().transform_to_another_object() #returns the tranfrmed object
    object_a = sample.get_a() # return the original Object
    new_object_b = sample.get_b().transform_to_another_object()
    object_b = sample.get_b()

that was my idea until now.

The class actually generates a large dictionary, which is always built the same way (with the same keys). The stored element should then be able to be transformed into a new object with a transform method. So from the XML element to an custom object.

I hope I have explained this in a reasonably understandable way.

Maybe it is the completely wrong approach, in any case I am glad about your help.

Update The original class is part of a backend of a web-based application. Normally the __init__ method of the class takes a path and then reads an xml file (this is always built the same way, so same tags). This file is then read and separated into its tags and stored in a dictionary. Each dictionary key contains an xml element. With a get method now the single xml elements are to be accessed, in order to be able to convert these afterwards into an own object.

0

There are 0 answers