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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:11:47+00:00 2026-06-11T08:11:47+00:00

I am looking for something in the following piece of code so that the

  • 0

I am looking for something in the following piece of code so that the isinstance() check afterwards evaluates to True in any case:

i = WonderfulClass()
classinfo_of_i = something(i)
isinstance(i, classinfo_of_i) # must evaluate to True

If type is your answer, I’d be grateful if you explained why. Is type the real counterpart of isinstance? Or, asked the other way round, can you think of a case where isinstance(i, type(i)) evaluates to False?

This question arose in the context of the simple way to check if the elements of a list or set are single type?, where we have to go through a sequence and check if all the sequence elements are of the same kind. In this context, elements will be compared to each other. This comparison could be based on type or based on isinstance.

Relevant documentation regarding isinstance(object, classinfo):

Return true if the object argument is an instance of the classinfo
argument

  • 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-11T08:11:48+00:00Added an answer on June 11, 2026 at 8:11 am

    “Is type the real counterpart of isinstance? Or, asked the other way round, can you think of a case where isinstance(i, type(i)) evaluates to False?”

    I can’t see any situation where isinstance(i, type(i)) would not return True.

    type() inspects and returns the type object from an instance so there’s no reason why the returned value would fail the isinstance() check.

    Digging into the source of cPython, we see that the code behind type() simply returns the type object attached to the instance:

    v = (PyObject *)o->ob_type;
    Py_INCREF(v);
    return v;
    

    while the first thing that the isintance() code does is check if the types match exactly (it would later move on to match against classes in the chain of inheritance):

    int
    PyObject_IsInstance(PyObject *inst, PyObject *cls)
    {
       _Py_IDENTIFIER(__instancecheck__);
       PyObject *checker;
    
       /* Quick test for an exact match */
       if (Py_TYPE(inst) == (PyTypeObject *)cls)
          return 1;
    

    Note that Py_TYPE is simply a macro that returns obj->ob_type which matches the return value of type(). This is defined in Include/object.h as:

    #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I'm looking for something that will allow me to replicate the following Perl
I was looking at a piece of code today and notice that this particular
I am looking at the following piece of code: void printd(int n) { if
Can someone recommend a simple c# code generator. I just looking something with methods
I'm looking for something to add to my .vimrc that would tell MacVim to
I'm looking for something that works like Lisp's arg-supplied-p variables, to help differentiate a
My XML file consists of a structure that resembles something like the following: <root>
I have the following javascript code that is trying to call a server side
Looking for something to do the following: Given input: s Expected output: t
I have the following code in a kernel module that walks up the process

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.