0

I have a dictionary of methods in Python that is inside of a definition. This def is called outside of the class using an object. Whenever the def is called I am returned the results of the last item in the dictionary. In this case I am returned the results of 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. I have done this => functions('spc.php') and it still returned results for `def spc_summary'. And those def's should not have the same results.

amlane86
  • 668
  • 4
  • 15
  • 24
  • What is the purpose of the `functions` dictionary? Are you trying to call a function whose name is stored inside of `phpfile`? – hochl Mar 13 '12 at 19:38
  • 5
    The `'spc_summary.php':self.spc_summary()` lines should be without the parens like `'spc_summary.php':self.spc_summary` and at `functions.get(filename)` you should use `functions.get(filename)()` – Dan D. Mar 13 '12 at 19:40
  • Yeah, you're effectively calling all those member functions while constructing the dictionary on **each method invocation**. – hochl Mar 13 '12 at 19:41

2 Answers2

2

Your function dict seems to be doing the wrong thing. While defining your dict you are mapping the keys to the function result instead of the function object. If you map it to the function object the function gets called when you invoke functions.get(filename)()

Your dict should probably be like below:

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
}
Puneet
  • 736
  • 9
  • 17
0

Dictionaries are unordered, so the last object returned from iterating over a dict will probably not be the last item inserted into that dict.

functions.get(filename) is going to take the current value of filename and look it up in functions. filename gets its value from self.phpfile, so in your example self.phpfile must be set to 'spc_summary.php'.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237