1

I'm having trouble understanding what is happening here with this code based on the explanation given below:

def route(self, path):
    def wrapper(handler):
        self.routes[path] = handler
        return handler # returns 

    return wrapper

Here is the explanation that was given:

"in the route method, you took a path as an argument [note: the path here is a dictionary as follows: {"/home": <function home at 0x1100a70c8>} ] and in the wrapper method you added this path in the self.routes dictionary as a key and the handler as a value"

Something does not make sense here. Here are the questions I have?

  1. is the wrapper(handler) inheriting its argument from route(..., path)?

  2. If the handler argument is already a dictionary, i.e {"/home": <function home at 0x1100a70c8>}, being passed from path, how then does self.routes[path] = handler set path as a key and handler as a value? Doesn't make sense to me.

user72686
  • 11
  • 2
  • the snippet you've shared shouldn't do anything. you have to trigger the inner func at the top level one and maybe return it for anything to happen – DevCl9 Oct 22 '20 at 07:00
  • This looks like part of a decorator method, which would be used as `@route("/home")` – Barmar Oct 22 '20 at 07:05
  • @DevCl9 yes. That is why I mentioned the dictionary example. The top level function would be triggered and that dictionary would be passed to the path parameter in the route function. – user72686 Oct 22 '20 at 07:27
  • 3
    https://realpython.com/primer-on-python-decorators/ – Barmar Oct 22 '20 at 07:28
  • *"is this wrapper() function builtin or is it custom?"* — This snippet `def`ines a function called "`wrapper`". Not sure what's supposed to be "builtin" about it?! – deceze Oct 22 '20 at 07:38
  • @deceze Ha yah sorry that was an absent minded question! – user72686 Oct 22 '20 at 07:47

0 Answers0