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 7934733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:43:20+00:00 2026-06-03T21:43:20+00:00

Is it possible to dynamically assign special methods, such as __getitem__ , to a

  • 0

Is it possible to dynamically assign special methods, such as __getitem__, to a class instance using setattr()? For example, if I have this:

class Example (object):
    pass

And then try this:

>>> example = Example()
>>> setattr(example, '__getitem__', lambda x: 1)

I get this:

>>> example['something']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Example' object has no attribute '__getitem__'

But of course this works just fine:

>>> example.__getitem__('something')
1

There’s obviously something going on here that I don’t understand regarding how Python does method lookups for this sort of thing. Do these methods have to be set on the class, rather than on the instance?

UPDATE:

So, I should make it clear than I know I can see this on the Example class…I was hoping there was a way to set them per-instance, but the consensus I’m seeing so far is You Can’t Do That.

  • 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-06-03T21:43:21+00:00Added an answer on June 3, 2026 at 9:43 pm

    The issue here is that __getitem__() is defined at a class-level, not at instance-level:

    >>> class Example (object):
    ...     pass
    ... 
    >>> example = Example()
    >>> setattr(Example, '__getitem__', lambda x, y: 1)
    >>> example['something']
    1
    

    If you need it to be instance-specific:

    >>> class Example(object):
    ...     def __getitem__(self, item):
    ...         return self._getitem(item)
    ... 
    >>> example = Example()
    >>> setattr(example, '_getitem', lambda x: 1)
    >>> example['something']
    1
    >>> example2 = Example()
    >>> setattr(example2, '_getitem', lambda x: 2)
    >>> example['something']
    1
    >>> example2['something']
    2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to dynamically instantiate a class using a variable? For example is
I have an <asp:CheckBoxList> using RepeatLayout=Flow in my page I dynamically assign values to.
Is it possible to dynamically discover the properties of a PHP object? I have
Is it possible to dynamically set a Spring MVC portlet:actionURL portlet:param using javascript? I
I need to create a CSS stylesheet class dynamically in JavaScript and assign it
Possible Duplicate: Dynamically creating/inserting into an associative array in PHP I have the following
I currently have this type of code: private void FillObject(Object MainObject, Foo Arg1, Bar
Is it possible to dynamically access properties by using #evaluate? I apologize in advance
Is it possible to dynamically generate an assign xpath from a variable and an
Possible Duplicate: Dynamically adding collapsible elements I've seen a few posts on this but

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.