Create an application as a class in the bottle framework

Source: Internet
Author: User
Tags instance method modifier modifiers

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.