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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:43:23+00:00 2026-06-16T14:43:23+00:00

I have this code: class A: def __init__(self): def method(self, item): print self, :

  • 0

I have this code:

class A:
    def __init__(self):
        def method(self, item):
            print self, ": Getting item", item
        self.__getitem__ = types.MethodType(method, self, self.__class__)

class B(object):
    def __init__(self):
        def method(self, item):
            print self, ": Getting item", item
        self.__getitem__ = types.MethodType(method, self, self.__class__)

Then this works fine:

a = A()
a[0]

But this does not:

b = B()
b[0]

raising TypeError.

I found that new-style classes look for magic methods in class __dict__ instead of instance __dict__ . Is this right? Why is it so? Do you know about any article explaining the ideas behind? I tried RTFM, but maybe not the right ones or did not catch the thing…

Thank you very much!
Paul

  • 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-16T14:43:24+00:00Added an answer on June 16, 2026 at 2:43 pm

    This is documented in the Python datamodel documentation: Special method lookup for new-style classes:

    For new-style classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary.

    and

    The rationale behind this behaviour lies with a number of special methods such as __hash__() and __repr__() that are implemented by all objects, including type objects. If the implicit lookup of these methods used the conventional lookup process, they would fail when invoked on the type object itself[.]

    So, because both hash(int) and hash(1) must work, special methods are looked up on the type instead of on the instance. If __hash__() was looked up straight on the object, hash(int) would be translated to int.__hash__(), and that would fail, because int.__hash__() is an unbound method and it expects to be called on an actual instance of int() (e.g. 1); so for hash(int), type.__hash__() should called instead:

    >>> hash(1) == int.__hash__(1)
    True
    >>> hash(int) == type.__hash__(int)
    True
    >>> int.__hash__()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: descriptor '__hash__' of 'int' object needs an argument
    

    This is a backwards-incompatible change, so it only applies to new-style objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
I have the following code in django.template: class Template(object): def __init__(self, template_string, origin=None, name='<Unknown
I have following code: class Foo(object): def __init__(self): baz=self.bar(10) @staticmethod def bar(n): if n==0:
Basically I have the following code: import multiprocessing import time class MyProcess(multiprocessing.Process): def __init__(self,
I tried this example code: class testclass: classvar = 'its classvariable LITERAL' def __init__(self,x,y):
I have code like this: class Debug(object): ... @property def ThreadAwareLogger(self): if not self.logger_instance_for_current_thread:
I have the following IronPython code. class Hello: def __init__(self): pass def add(self, x,
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
I have this code: class SomeClass: @classmethod def func1(cls,arg1): #---Do Something--- @classmethod def func2(cls,arg1):
I have this code: class DocumentIdentifier attr_reader :folder, :name def initialize( folder, name )

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.