Bottle framework and OOP, using method instead of function

后端 未结 5 1809
慢半拍i
慢半拍i 2020-12-04 10:46

I\'ve done some coding with Bottle. It\'s really simple and fits my needs. However, I got stick when I tried to wrap the application into a class :

import bo         


        
5条回答
  •  日久生厌
    2020-12-04 11:32

    You have to extend the Bottle class. It's instances are WSGI web applications.

    from bottle import Bottle
    
    class MyApp(Bottle):
        def __init__(self, name):
            super(MyApp, self).__init__()
            self.name = name
            self.route('/', callback=self.index)
    
        def index(self):
            return "Hello, my name is " + self.name
    
    app = MyApp('OOBottle')
    app.run(host='localhost', port=8080)
    

    What most examples out there are doing, including the answers previously provided to this question, are all reusing the "default app", not creating their own, and not using the convenience of object orientation and inheritance.

提交回复
热议问题