I am adding a URL handler to a Flask application using add_url_rule. It requires a function as a handler (not a method). However, for proper encapsulation, my intended function is a bound method that uses the self variable. Is there any way I can somehow make Flask see my function: def action(self, param) as def action(param) ?
class A(): def __init__(self): self._localvar = 5 self._flask = Flask("testapp") def add_rules(self): self._flask.add_url_rule("/","root",????) def action(self, value): print("The value is {}".format(self._localvar)) def run(self): self._flask.run() app = A() app.add_rules() app.run() what I want to do is replace ???? with app.action, a bound method. However, add_url_rule expects a function. I tried app.action.__func__ and app.action.im_func which are function pointers. However, the function signature is still going to require self and will give me a runtime error.
My current solution is to have some loose functions in my module and have app just use them directly, but it just seems that there has to be a cleaner way since the handlers are really tightly coupled to the app object and I don't want them floating around outside of it.
selfgoing to reference then? If you have a single, global instance, usea = A()and registera.action.app.action?def action(value)to thedef action(self, value)and somehow smartly replace theselfreferences with the address of myselfsuch that this method becomes compatible with the register typeAclass, but you don't seem to have an instance ofAanywhere.appis an instance ofFlask, an entirely unrelated class. WhatAinstance do you want to call theactionmethod of?