Can functions be used as dictionaries value | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Can functions be used as dictionaries value

I am planning a python project on Mad Libs and i wondered if functions can be used as dict value. e.g dict{ market : example() coding_experience : code() } print(dict[market])

20th Jun 2021, 7:36 PM
Haxsys - avatar
2 Answers
+ 3
Well, why not just try it? But I don't understand yet why you want to do it like that. Would a custom class or own module script or something do it?
20th Jun 2021, 8:01 PM
Lisa - avatar
+ 2
if you use dict constructor: func = dict(market=example, coding_experience=code) if you use dict literal: func = { "market": example, "coding_experience": code, } in neither case you use parenthesis after function name (else you assign the function call return value instead of the function itself ^^)
20th Jun 2021, 8:53 PM
visph - avatar