Python 2 中的函数字典
Posted
技术标签:
【中文标题】Python 2 中的函数字典【英文标题】:Dictionary of functions in Python 2 【发布时间】:2019-03-04 15:51:01 【问题描述】:我有一本函数字典。我希望能够使用相同的键来访问函数字典。有没有办法在 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')
【问题讨论】:
您能否提供您目前拥有的代码以及您尝试解决问题的方法? 您定义的所有函数都将在国际范围内存储在字典中,并且也可以这样做,只是不能直接在字典中定义命名函数。 【参考方案1】:是的,函数是 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
【讨论】:
这很棒。我不明白为什么这个问题的谷歌搜索点击网站都没有解决这种使用字典的问题。【参考方案2】:是的,函数是一流的对象,因此它们可以用于对象可以用于的任何事情。
dict_of_functions = 'a': lambda: print('a'),
'b': lambda: print('b')
dict_of_functions['a']()
# 'a'
【讨论】:
【参考方案3】:你可以试试下面的代码:
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
希望这能回答你的问题!!!
【讨论】:
以上是关于Python 2 中的函数字典的主要内容,如果未能解决你的问题,请参考以下文章