Cloud Stack Ninja

import inspect


class Format:
    def __init__(self):
        self.div=self.div()

    def div(self, elem):
        return "<{}>{}</{}>".format((inspect.currentframe().f_code.co_name),
                                    elem,(inspect.currentframe().f_code.co_name))

    class div(object):
        def __init__(self):
            pass
        def div(self,elem):
            return"<{}></{}><{}>{}</{}>".format("div", "div",elem ,"div","div")


format=Format()
print(format.div.div("pop"))  
# -> **I receive <div></div>pop<div></div>**
print(format.div("pop"))     
** # -> I want to receive <div>pop</div>**

This is the error I get:

TypeError: 'div' object is not callable.

the wanted result <div>pop</div> when i execute format.div("pop")



Read more here: https://stackoverflow.com/questions/64405690/how-to-execute-a-method-with-the-same-name-as-an-inner-class

Content Attribution

This content was originally published by User20 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: