首页 > 解决方案 > Python 2 中的函数字典

问题描述

我有一本函数字典。我希望能够使用相同的键来访问函数字典。有没有办法在 Python 2 中做到这一点?

现在可以关闭了。我从下面发现了我的编码错误!

我有一个现有的语法检查器,用于由多个编辑器开发的一组降价文件。我们已经意识到现有的语法要求没有足够的特异性来确保编辑器之间内容的一致性。下面的代码摘录与将这些新要求添加到该检查器有关。我没有包含任何现有的检查器代码,因为它与函数字典的使用无关。我在每个部分的前面添加了一个单行注释来指定代码功能。运行该 check_routine 不会“调用”所需的低级例程:Term_period


class xyz_syntax_checker(object):

# syntax requirements of needed tests for each markdown marker data: LD through SSM

    self.LD_Req = {"LD1" : "Grk",   "LD2" : "NA",   "MSep":"comma", "Rqd":"NA", "AD1":"NA", "AD2":"NA", "Term":"NA"}
    self.WD_Req = {"LD1" : "NA",    "LD2" : "NA",   "MSep":"NA",    "Rqd":"NA", "AD1":"NA", "AD2":"NA", "Term":"NA"}
    self.ST_Req = {"LD1" : "Grk",   "LD2" : "NA",   "MSep":"NA",    "Rqd":"NA", "AD1":"NA", "AD2":"NA", "Term":"NA"}
    self.AS_Req = {"LD1" : "Grk",   "LD2" : "NA",   "MSep":"comma", "Rqd":"NA", "AD1":"semicolon",      "AD2":"NA", "Term":"period"}
    self.PP_Req = {"LD1" : "NA",        "LD2" : "NA",   "MSep":"NA",    "Rqd":"NA", "AD1":"NA", "AD2":"NA", "Term":"NA"}
    self.PO_Req = {"LD1" : "UGG",   "LD2" : "NA",   "MSep":"comma", "Rqd":"NA", "AD1":"NA", "AD2":"NA", "Term":"period"}
    self.IN_Req = {"LD1" :"number",          "LD2" : "NA",  "MSep":"NA",    "Rqd":"NA", "AD1":"NA", "AD2":"NA", "Term":"period"}
    self.AL_Req = {"LD1" : "YN",    "LD2" : "NA",   "MSep":"NA",    "Rqd":"NA", "AD1":"NA", "AD2":"NA", "Term":"period"}
    self.ET_Req = {"LD1" : "LL",    "LD2" : "NA",   "MSep":"comma", "Rqd":"colon",      "AD1":"colon",  "AD2":"NA", "Term":"period"}
    self.LX_Req = {"LD1" : "HL",    "LD2" : "CL",   "MSep":"comma", "Rqd":"NA", "AD1":"colon",  "AD2":"semicolon",      "Term":"period"}
    self.TP_Req = {"LD1" : "NA",    "LD2" : "NA",   "MSep":"NA",    "Rqd":"NA", "AD1":"NA", "AD2":"NA", "Term":"period"}
    self.RW_Req = {"LD1" : "LL",    "LD2" : "NA",   "MSep":"comma", "Rqd":"colon",      "AD1":"semicolon",  "AD2":"NA", "Term":"period"}
    self.AN_Req = {"LD1" : "LL",        "LD2" : "NA",   "MSep":"comma", "Rqd":"colon",  "AD1":"semicolon",  "AD2":"NA", "Term":"period"}
    self.SN_Req = {"LD1" : "LL",        "LD2" : "NA",   "MSep":"comma", "Rqd":"colon",      "AD1":"semicolon",  "AD2":"NA", "Term":"period"}
    self.SS_Req       = {"LD1" : "NA",  "LD2" : "NA",   "MSep":"NA",    "Rqd":"NA", "AD1":"NA", "AD2":"NA", "Term":"NA"}
    self.SSM_Req    = {"LD1" : "SSL",   "LD2" : "NA",   "MSep":"NA",    "Rqd":"NA", "AD1":"NA", "AD2":"NA", "Term":"colon"}

# hash table map

    self.All_Req      = {
        "LD":self.LD_Req, 
        "WD":self.WD_Req, 
        "ST":self.ST_Req, 
        "AS":self.AS_Req, 
        "PP":self.PP_Req, 
        "PO":self.PO_Req, 
        "IN":self.IN_Req, 
        "AL":self.AL_Req, 
        "ET":self.ET_Req, 
        "LX":self.LX_Req, 
        "TP":self.TP_Req, 
        "RW":self.RW_Req, 
        "AN":self.AN_Req, 
        "SN":self.SN_Req, 
        "SS":self.SS_Req, 
        "SSM":self.SSM_Req}

# dummy examples of low-level defs that will do the work - only Term_period has content

    def Term_NA (self):  
        return ''

    def Term_colon (self):  
        return ''

    def Term_period (self):  
        if self.Current_Marker == '':
            return ''
        print 'len tmd,cm',len(self.marker_data[self.Current_Marker]),self.Current_Marker
        this_marker_data = self.marker_data[self.Current_Marker].rstrip()
        lastchar = this_marker_data[-1]
        print 'Current marker',self.Current_Marker,'data does not end in a period'
        return ''

# hash table for those low-level defs - f(requirements)

        self.action_mapping = {
                "Term_NA" : self.Term_NA(),
                "Term_colon" : self.Term_colon(),
                "Term_period" : self.Term_period()
        }

# intermediate def to call those - amalgamation of common logic for each call

       def call_action_key(self,this_action):
            self.Action_Key  = this_action + '_' + self.All_Req[self.Current_Marker][this_action]
            print 'ta,AK',this_action,self.Action_Key 
            o = self.action_mapping[self.Action_Key]
            return o

# pseudo top-level def for this new checking/testing

        def checker_routine(self):
            self.Current_Marker = 'PO'
            o = self.call_action_key.get('Term')

标签: pythondictionaryuser-defined-functions

解决方案


你可以试试下面的代码:

class X():

     def funcA(self):
         return "A"

     def funcB(self):
         return "B"

     def field_map(self,x):
         # Dictionary of Functions
         mapping = {
         "func_A":self.funcA(), 
         "func_B":self.funcB()
         }
        return mapping.get(x,None)


x = X()
z = x.field_map("func_A")
print z # Will print Output of func_A()
z = x.field_map("func_B")
print z # Will print Output of func_B()
z = x.field_map("func_OOB")
print z # Will print None as no function is found

输出:

A
B
None

希望这能回答你的问题!!!


推荐阅读