Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7653855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:07:45+00:00 2026-05-31T12:07:45+00:00

In python dictionary, can function and instances of the class stored as the key

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:07:47+00:00Added an answer on May 31, 2026 at 12:07 pm

    Both instances and functions can be keys:

    In [151]: def foo(): pass
    
    In [152]: {foo:1}
    Out[152]: {<function __main__.foo>: 1}
    
    In [153]: {foo:1, Fake(): 2}
    Out[153]: {<__main__.Fake at 0x3e04160>: 2, <function __main__.foo>: 1}
    
    In [154]: Fake
    Out[154]: __main__.Fake
    
    In [155]: type(Fake)
    Out[155]: type
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to call a function in python using a dictionary with matching key-value
In Python, how can I simply do the equivalent of dictionary.get(key, default) for lists
Python's setdefault allows you to get a value from a dictionary, but if the
In Python I can use get method to get value from an dictionary without
I have data in a python dictionary that I'd like to store in a
A Python function object has an attribute dictionary called func_dict which is visible from
How can I bind arguments to a Python function so that I can call
I'm using the Python execfile() function as a simple-but-flexible way of handling configuration files
In Python I can define a function as follows: def func(kw1=None,kw2=None,**kwargs): ... In this
I've got this python dictionary mydict, containing arrays, here's what it looks like :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.