Why is self superfluous for a method when using bottle in a class?
Question: I usually use `bottle` in a naked script:

    
    
    import bottle
    
    @bottle.route('/ping')
    def ping():
        return "pong"
    
    bottle.run()
    

It works fine, a call to `http://127.0.0.1:8080/ping` returns `pong`. I now
want to use a class for the same functionality:

    
    
    import bottle
    
    class PingPong:
        @bottle.route('/ping')
        def ping(self):
            return "pong"
    
        def run(self):
            bottle.run()
    
    if __name__ == "__main__":
        p = PingPong()
        p.run()
    

A call to `http://127.0.0.1:8080/ping` now returns a `500` and the traceback
on the server is

    
    
    Traceback (most recent call last):
      File "C:\Python34\lib\site-packages\bottle.py", line 862, in _handle
        return route.call(**args)
      File "C:\Python34\lib\site-packages\bottle.py", line 1732, in wrapper
        rv = callback(*a, **ka)
    TypeError: ping() missing 1 required positional argument: 'self'
    127.0.0.1 - - [28/Dec/2015 19:15:15] "GET /ping HTTP/1.1" 500 745
    

If I remove `self` from the method definition the server works fine.

Why is the self parameter superfluous in this case? How is that different from
a normal method where self is passed by default and corresponds to 'no
parameters' in the call of the method?


Answer: That's because bottle does not know that the function you have passed is a
method, it has no notion of methods. Also, ask yourself: should bottle
automatically create instances?

If you want to use a bound instance method, do this instead:

    
    
    class PingPong:
        def ping(self):
            return "pong"
    
        def run(self):
            bottle.route('/ping', callback=self.ping)
            bottle.run()
    
    if __name__ == "__main__":
        p = PingPong()
        p.run()
    

That is, pass the bound method to `route()` once the instance has been
initialized.

