Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 使用dict-like-C开关_Python 2.7 - Fatal编程技术网

Python 2.7 使用dict-like-C开关

Python 2.7 使用dict-like-C开关,python-2.7,Python 2.7,它总是返回None,我想得到一个值 这是使用dict?的正确方法,key\u check中没有return语句,因此它自然不会返回任何内容。您基本上错过了实现的最后一点:一旦您按名称查找适当的函数,您需要调用该函数并返回结果 class checkevent: def __init__(self,fromuser): self.fromuser = fromuser def openid_check(self):# use sqlalchemy exist_user = User.qu

它总是返回None,我想得到一个值
这是使用dict?

的正确方法,
key\u check
中没有
return
语句,因此它自然不会返回任何内容。您基本上错过了实现的最后一点:一旦您按名称查找适当的函数,您需要调用该函数并返回结果

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


从技术上讲,如果您真的愿意,您可以将所有这些内容串联到一条语句中,但除非性能处于最佳状态,否则我不会推荐它,因为可读性会受到影响。

def key\u check(self,x)
不会返回任何内容。这就是为什么您会得到
。还有一个问题,为什么我不能在grade函数中调用openid_check(),为什么dict使用像{'grade':self.grade}而不是{'grade':grade}
openid_check
grade
都是实例方法,所以你需要在实例上调用它们(
self
)。如果你对此有更多的问题,这是一个完全不同的主题,有大量的在线资源(只需谷歌“python实例方法”)。
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