class checkevent:
def __init__(self,fromuser):
self.fromuser = fromuser
def openid_check(self):# use sqlalchemy
exist_user = User.query.filter_by(openid = self.fromuser).first()
if exist_user is None:
text = u'请绑定后在使用'
return text
def grade(self):
openid_check()
exist_user = User.query.filter_by(openid = self.fromuser).first()
geturp = urp(exist_user.username, exist_user.password_urp) #the function
return geturp #return the grades as text
def key_check(self,x): # use dict like switch
{'grade': self.grade
}
contents = checkevent('ozvT4jlLObJWzz2JQ9EFsWSkdM9U').key_check('grade')
print contents
总是返回None,我想得到一个值 这是使用dict的正确方法吗?
答案 0 :(得分:2)
return
中没有key_check
语句,所以很自然它不会返回任何内容。您基本上缺少实现的最后一点:一旦按名称查找相应的函数,您需要调用该函数并返回结果。
def key_check(self, key): # "x" is a meaningless name; use something meaningful
lookup = {
'grade': self.grade
}
func = lookup[key] # Look up the correct method
return func() # Call that method and return its result
从技术上讲,如果你真的想要,你可以将所有内容整合到一个声明中,但除非性能非常优惠,否则我不会推荐它,因为可读性会受到影响。