i具有函数字典。我希望能够使用相同的密钥访问函数字典。有什么方法可以在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')
是的,功能是Python中的一流对象。您可以直接在使用lambda
s的字典定义中设置小函数,也可以提前定义功能并引用它们:
In [56]: def plus3(x):
...: return x+3
In [57]: d = {'plus1': lambda x: x+1, 'plus3': plus3}
In [58]: d['plus1'](10)
Out[58]: 11
In [59]: d['plus3'](20)
Out[59]: 23
dict_of_functions = {'a': lambda: print('a'),
'b': lambda: print('b')}
dict_of_functions['a']()
# 'a'
您可以尝试以下代码:
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
希望这回答您的问题!