Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
call func from dict
#1
how do I call functions from a dictionary?
def foo():
    print('foo')

def bar():
    print('bar')

foobar = {
    'foo': foo,
    'bar': bar
}

command = input('foo or bar, Choose one. ')

foobar[command] # returns nothing.
print(foobar[command]) # prints memory location.
Output:
PS C:\Users\User\Desktop> python test.py foo or bar, Choose one. foo <function foo at 0x0380C030> PS C:\Users\User\Desktop>
Reply
#2
foobar[command]()
Reply
#3
I'm still not getting this. It works when I call functions from within the same program but if I import the functions then store them in a dict they get ran even without being called. Why?
I am trying to modify this program to get rid of all the if\elif\else statements.
Reply
#4
Use if __name__ == '__main__':
# my_nodule
def foo():
    print('foo')

def bar():
    print('bar')

foobar = {
    'foo': foo,
    'bar': bar
}

if __name__ == '__main__':
    command = input('foo or bar, Choose one. ')
    foobar[command]()
No do not get run on import.
>>> import my_module

>>> command = input('foo or bar, Choose one. ')
foo or bar, Choose one. bar

>>> my_module.foobar[command]()
bar
Another way that i think is cleaner as avoid the the somewhat strange call [something]() and do error checking with get().
# my_module1.py
def spam():
    return 'spam'

def eggs():
    return 'eggs'

def switch_case(user_choice):
    return {
        'foo': spam(),
        'bar': eggs(),
    }.get(user_choice, f'<{user_choice}> Not in Record')
>>> import my_module1

>>> command = input('foo or bar, Choose one. ')
foo or bar, Choose one. bar
>>> my_module1.switch_case(command)
'eggs'

>>> command = input('foo or bar, Choose one. ')
foo or bar, Choose one. car
>>> my_module1.switch_case(command)
'<car> Not in Record'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to output one value per request of the CSV and print it in another func? Student44 3 1,277 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Func Animation not displaying my live graph jotalo 0 1,521 Nov-13-2020, 10:56 PM
Last Post: jotalo
  Trying to write func("abcd") -> "abbcccdddd" omm 8 3,981 Oct-24-2020, 03:41 AM
Last Post: bowlofred
  Sort a dict in dict cherry_cherry 4 62,313 Apr-08-2020, 12:25 PM
Last Post: perfringo
  call dict object result key error lateublegende 2 2,986 May-15-2019, 01:08 PM
Last Post: lateublegende
  About [from FILE import FUNC] Nwb 7 3,522 Apr-21-2019, 02:46 PM
Last Post: snippsat
  Executing func() from a different folder ebolisa 2 2,305 Jan-14-2019, 10:18 AM
Last Post: ebolisa
  Correct number wrong position func. albry 5 5,956 Jan-11-2019, 04:01 PM
Last Post: Larz60+
  How can I return my list from a func? Mike Ru 3 3,040 Oct-22-2018, 01:15 PM
Last Post: buran
  list func in lambda mepyyeti 1 2,879 Mar-10-2018, 09:07 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020