In the bottle framework, the request is routed to the corresponding response method using the route modifier. This feature is ideal for routing requests and methods, but it is troublesome to use classes in bottle to create applications.
Class Sanyou:
app = Bottle ()
@app. Route ('/')
def homepage (self): return
"welcome!"
App1 = Sanyou (). App
app2 = Sanyou.app
The above code, whether App1 or APP2, returns Typeerror:homepage () takes exactly 1 argument (0 given)
This is because homepage is not bound to the Sanyou instance in the above code, that is, as a modifier of the routing method, the instance method homepage is encapsulated in advance, causing self not to pass in the homepage method, so homepage is not bound to the sanyou instance.
Class Sanyou:
app = Bottle ()
@classmethod
@app. Route ('/')
def homepage (CLS): Return
"Welcome !"
App = Sanyou.app
Similarly, because the route modifiers encapsulate the class method in the above code, the CLS cannot be passed into the homepage method, resulting in homepage not being a class method. Note that changing the modifier order also does not allow two modifiers to be properly encapsulated.
Class Sanyou:
app = Bottle ()
@app. Route ('/')
@classmethod
def homepage (CLS): Return
" welcome! "
App = Sanyou.app
Execute the above code and you will get "TypeError: ' Classmethod ' object is not callable."
Therefore, if you want to use modifiers for routing in your class, you can only use static methods, such as:
# code 1
class Sanyou:
app = Bottle ()
@staticmethod
@app. Route ('/')
def homepage (): Return
" welcome! "
App = Sanyou.app
----------------------------------------------
# code 2
class Sanyou:
app = Bottle ()
@app. Route ('/')
def homepage (): Return
"welcome!"
App = Sanyou.app