I'm new to Python.
When I started making a web application because I could easily make a web application with Bottle, I was addicted to hitting the method of the instance.
Most commentary sites have the following description.
@route('/api')
def api_action():
...
I wanted to hit the method of a specific class instance in this api_action, but beginners got hooked here.
Specifically, I wanted to hit an instance of a DB connection.
As a result of various googles, I settled down as follows.
from bottle import route
class Sample:
def __init__(self, dbcon):
self.dbcon = dbcon
route('/api/<arg>')(self.instance_method)
def instance_method(self, arg):
# <self.dbcon>Processing using
You hit the bottle's route method directly without using a decorator.
Recommended Posts