简体   繁体   English

如何在Python中检索字典值?

[英]How to retrieve dictionary values in Python?

I have a dictionary of methods in Python that is inside of a definition. 我在Python中有一个方法词典,它在一个定义中。 This def is called outside of the class using an object. 使用对象在类外部调用此def Whenever the def is called I am returned the results of the last item in the dictionary. 每当调用def时,我都会返回字典中最后一项的结果。 In this case I am returned the results of def spc_summary: . 在这种情况下,我返回def spc_summary:的结果def spc_summary:

 def setStyles(self):
    # Assign function to file
    functions = {
            'app_server.php':self.app_server(),
            'dcrm2nlyte.php':self.dcrm2nlyte(),
            'export.php':self.export(),
            'host_server.php':self.host_server(),
            'spc.php':self.spc(),
            'spc_approved.php':self.spc_approved(),
            'spc_approved_by_dc.php':self.spc_approved_by_dc(),
            'spc_by_dc.php':self.spc_by_dc(),
            'spc_complete.php':self.spc_complete(),
            'spc_summary.php':self.spc_summary()
    }

    filename = self.phpfile
    functions.get(filename)

Can someone please explain what is happening here? 有人可以解释一下这里发生了什么吗? Let me know if more detail is required. 如果需要更多详细信息,请与我们联系。 Thanks! 谢谢!

Let me add some detail: The functions.get(filename) is retreiving the last dictionary item reguardless of what filename is. 让我添加一些细节: functions.get(filename)正在检索最后一个字典项,而不管文件名是什么。 I have done this => functions('spc.php') and it still returned results for `def spc_summary'. 我做了这个=> functions('spc.php') ,它仍然返回`def spc_summary'的结果。 And those def's should not have the same results. 那些def不应该有相同的结果。

Your function dict seems to be doing the wrong thing. 你的函数dict似乎做错了。 While defining your dict you are mapping the keys to the function result instead of the function object. 在定义dict时,您将键映射到函数结果而不是函数对象。 If you map it to the function object the function gets called when you invoke functions.get(filename)() 如果将它映射到函数对象,则在调用functions.get(filename)()时将调用该函数.get functions.get(filename)()

Your dict should probably be like below: 你的dict应该如下所示:

functions = {
        'app_server.php':self.app_server,
        'dcrm2nlyte.php':self.dcrm2nlyte,
        'export.php':self.export,
        'host_server.php':self.host_server,
        'spc.php':self.spc,
        'spc_approved.php':self.spc_approved,
        'spc_approved_by_dc.php':self.spc_approved_by_dc,
        'spc_by_dc.php':self.spc_by_dc,
        'spc_complete.php':self.spc_complete,
        'spc_summary.php':self.spc_summary
}

Dictionaries are unordered, so the last object returned from iterating over a dict will probably not be the last item inserted into that dict. 字典是无序的,因此迭代在dict上返回的最后一个对象可能不是插入该字典的最后一个项目。

functions.get(filename) is going to take the current value of filename and look it up in functions . functions.get(filename)将获取filename的当前值并在functions查找。 filename gets its value from self.phpfile , so in your example self.phpfile must be set to 'spc_summary.php'. filenameself.phpfile获取其值,因此在您的示例中, self.phpfile必须设置为'spc_summary.php'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM