Finally I fix this ask use code like this:(2018-06-03)
class Main:
@app.route('^/$')
def default(self):
return 'hello from class ccc'
module=sys.modules[func.__module__]
cls=getattr(module,func.__qualname__.replace('.'+func.__name__,''))
ins=cls()
m=getattr(cls,func.__name__)
resp.body=m(cls) #func.__module__+'.'+func.__qualname__+' func:'+func.__name__
That's not pyhonic right?I'm new guy to python
//////old
class D:
def __init__(self):
self.handlers={}
def check(self,func):
self.handlers['h']=func
def decorator(*args,**kwargs):
return func(*args,**kwargs)
return decorator
def call(self,p):
return self.handlers['h'](p)
d=D()
class Test:
@d.check
def prt(self,v):
print(v)
t=Test()
d.call(123)
There is error info:prt() missing 1 required positional argument: 'v'
It seems need a parameter named 'self' but how can i pass it?
//edit (2018-06-01)
Thanks to all.I ask this cause I try write a python web framework.And I want route to a method of class like below
app=MyFrm()
class Controller:
@app.route('/')
def hello():
return 'hello world'
But existing do like below.It's no need or nobody to do this in python?
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
Now I fixed this problem use @staticmethod