python 我在类中做了个闭包..出错了
class yoyo(object):
def __init__(self):
self.is_logined = False
def login(self):
print "do login"
self.is_logined = True
def login_required(self,func):
@functools.wraps
def wrapper(self,*args,**kwargs):
return func(self,*args,**kwargs)
if self.is_logined:
return wrapper
self.login()
return wrapper
@login_required
def test_login(self):
print 'testing:login'
代码出错: login_required() takes exactly 2 arguments (1 given)
第一次在类中写闭包,请教哪里出错了
喵咪木ちゃん
10 years, 3 months ago
Answers
正确答案 :
class yoyo(object):
def __init__(self):
self.is_logined = False
def login(self):
print "do login"
self.is_logined = True
def login_required(self,func):
@functools.wraps(func)
def wrapper(self,*args,**kwargs):
return func(self,*args,**kwargs)
if self.is_logined:
return wrapper
self.login()
return wrapper
@login_required
def test_login(self):
print 'testing:login'
setse
answered 10 years, 3 months ago