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

  • Home
  • SEARCH
  • 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 8774629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:36:59+00:00 2026-06-13T18:36:59+00:00

I am using python 2.7.3 on Windows. I tried to override the __instancecheck__ magic

  • 0

I am using python 2.7.3 on Windows. I tried to override the __instancecheck__ magic method as a class method. But I can not make it work.

class Enumeration(int):
    @classmethod
    def __instancecheck__(cls, inst):
        if type(inst) == cls:
            return True
        if isinstance(inst, int) and inst in range(0,10):
            return True
        return False

print isinstance(1, Enumeration)   # prints False
print isinstance(1, Enumeration()) # prints True

I assume the first print statement would get True. But it seems the magic method __instancecheck__ is not called. And I don’t know why the second print statement can work since the isinstance should take a class/type as the second parameter.

Does anyone know what the problem is? Thanks.

  • 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-13T18:37:01+00:00Added an answer on June 13, 2026 at 6:37 pm

    instancecheck must be defined in a metaclass:

    class Enumeration(type):
        def __instancecheck__(self, other):
            print 'hi'
            return True
    
    
    class EnumInt(int):
        __metaclass__ = Enumeration
    
    print isinstance('foo', EnumInt) # prints True
    

    Why is that? For the same reason why your second example worked. When python evaluates isinstance(A, B) it assumes B to be an object, looks for its class and calls __instancecheck__ on that class:

    isinstance(A, B):
        C = class-of(B)
        return C.__instancecheck__(A)
    

    But when B is a class itself, then its class C should be a class of a class, in other words, a meta-class!

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

Sidebar

Related Questions

I'm trying to make a HTTP request using python. I tried changing my windows
How can I write to files using Python (on Windows) and use the Unix
i have installed lxml2.2.2 on windows platform(i m using python version 2.6.5).i tried this
I want to take a screenshot using python. I have tried using PIL, but
I'm trying to make a simple hello-world executable python gui app in windows using
I'm using Python 2.7.3 on Windows XP via the IDLE GUI, and I'm trying
I am using Python 3.1 on a Windows 7 machine. Russian is the default
I'm using python 2.5 in windows on a macbook pro with IDLE. How do
I want to write text files with DOS/Windows line endings '\r\n' using python running
Is there any way to bind two windows from seprate processes together using Python/Pygame?

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.