In python dictionary, can function and instances of the class stored as the key value pair? I tried to store function and instances of classes as dictionary value but it won’t work. Are there work-around methods around it or is it that dictionary is not meant for storing the instances of classes and function?
Just for the verification,
# I created a dictionary
diction = {'god': 'greed', 'hero': 'zero', 'human': 'pain'}
# and a simple class
class checker:
def __init__(self):
pass
# and created a instance of that class
obTry = checker()
# and when I try to add this as
diction['new':obTry]
it gave me TypeError.
Just realized that I was the one doing silly mistake, I should have added it like
diction['new'] = obTry
My mistake for not paying attention to syntax.
Both instances and functions can be keys: