Question

I'm creating a simple web.py service like this

urls = (
    '/test', 'index'
)

class index:
    def GET(self):
        user_data = web.input()
        return performstuff(user_data.color, user_data.shade)

    def performstuff(color, shade):
        return "color is " + color + " shade is: " + shade

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

When I run this and go to : http://:8080/test?color=red&shade=dark I get an error global name 'performstuff' is not defined

How can I resolve this? Basically I am trying to create another function in my class so it can do some business logic and then I can simply return its results in my GET method.

Was it helpful?

Solution

For python objects, member functions need to be referenced with self, i.e.:

def GET(self):
    user_data = web.input()
    return self.performstuff(user_data.color, user_data.shade)

OTHER TIPS

performstuff is a method of class index, therefore, you have to preface it with self.

def GET(self):
    user_data = web.input()
    return self.performstuff(user_data.color, user_data.shade)

@Anthony

In order to access the performstuff method of class doit, you could either create an instance of the class (myInstance = doit(); myInstance.performstuff(), or add @classmethod above the method declaration and then say doit.performstuff()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top