-3

I do have a map:

foo_map = {
'func1': sky()
'func2': tree()
'fnuc3': ground()
}

I do have a foobar_list = ['func2', 'fnuc3']

I do have methods too:

d

ef sky():
    print "sky"

def tree():
    print "tree"

def ground():
    print "ground"

I do want to do as follows:

for element in foobar_list:
    foo_map[element]

The expected result is to call methods assigned to 'func2', 'fnuc3' so tree() and ground() without calling sky(). Although, sky() and ground() and tree() are called when foo variable is created because I understand memory is allocated for this variable object hence the calls are executed.

How to achieve the above without calling sky()???

EDIT:

My question is not duplicate of the highlighted duplicated question. My question has been asked with different keywords and duplicate question has not been recommended as suggested answer when I typed my question. Technically, both questions are the same but the questions are not the same from natural language perspective.

Dariusz Krynicki
  • 2,544
  • 1
  • 22
  • 47
  • 1
    Don't put `()` after the function names inside your dict. That _calls_ the functions. You just want to reference them. – khelwood Feb 07 '18 at 13:34
  • Thank you. This answer is awsome. So simple thing gave me so much headache. PS. I do disagree with whoever marked is as duplicate because I have been looking for answer but could not find right keyword to search for the problem due to lack of knowledge. So, I am grateful to @khelwood for being helpful and putting me back on track. I am disrespectful to whoever marked it down and set penalty for me because that person is not helpful at all and I do not understand of the objective behind that person action. Cheers. – Dariusz Krynicki Feb 07 '18 at 13:37
  • 1
    Just because you couldn't *find* the duplicate doesn't mean it's not a duplicate. It's correct to close it as such. – deceze Feb 07 '18 at 13:38
  • hey khelwood, I was not unable to execute function call after iteration. I was missing () still as suggested by deceze below. – Dariusz Krynicki Feb 07 '18 at 13:47
  • @deceze, do you suggest that: if questionA =/= questionB and answer to questionA is in answer to questionB => questionA == questionB ??? My answer to you is: no, question A is different from question B even if answer to questionA is included in the answer to questionB. So, my question is not a duplicate of the question you quoted. – Dariusz Krynicki Feb 07 '18 at 13:52
  • The questions are 99% the same and the answer in the duplicate most certainly hits your nail on the head. If you required 100% sameness for duplicates there would be none at all. – deceze Feb 07 '18 at 13:57
  • Yes, I do agree with you that technically the both questions are the same. Although, from natural language perspective the both questions are not the same and are asked with different keywords. This tells me this two questions are not the same. Furthermore, you penalized my question first without explaining the argument (showing the example you used) till you have been called to show the duplicated question. So, I think your behaviour is wrong. It is great to hear you know all the questions in SO. SO recommendation engine did not highlight your question when I typed mine. – Dariusz Krynicki Feb 07 '18 at 14:07
  • You may be making too many connections there. I answered your question; simultaneously another user voted to close it as a duplicate they knew would fit; who knows who downvoted or why… These things are all orthogonal. And how the question was phrased is irrelevant; again, that is not what makes it a duplicate, the *topic* determines the duplicate. In fact, differently phrased duplicates serve to point more users towards the canonical answer using different keywords. And I don't understand what you mean with "called to show the duplicate". – deceze Feb 07 '18 at 14:25

1 Answers1

1

Although, sky() and ground() and tree() are called when foo variable is created because I understand memory is allocated for this variable object hence the calls are executed.

No, they're executed because you executed them by writing sky(). What you want is to merely store a reference to the function in the dict and then call it later:

foo_map = {'func1': sky, ...}
                       ↑
foo_map[element]()
                ↑↑
deceze
  • 510,633
  • 85
  • 743
  • 889