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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:35:56+00:00 2026-05-30T22:35:56+00:00

I want to test whether an object is an instance of a class, and

  • 0

I want to test whether an object is an instance of a class, and only this class (no subclasses). I could do it either with:

obj.__class__ == Foo
obj.__class__ is Foo
type(obj) == Foo
type(obj) is Foo

Are there reasons to choose one over another? (performance differences, pitfalls, etc)

In other words: a) is there any practical difference between using __class__ and type(x)? b) are class objects always safe for comparison using is?


Update: Thanks all for the feedback. I’m still puzzled by whether or not class objects are singletons, my common sense says they are, but it’s been really hard to get a confirmation (try googling for “python”, “class” and “unique” or “singleton”).

I’d also like to clarify that, for my particular needs, the “cheaper” solution that just works is the best, since I’m trying to optimize the most out of a few, specialized classes (almost reaching the point where the sensible thing to do is to drop Python and develop that particular module in C). But the reason behind the question was to understand better the language, since some of its features are a bit too obscure for me to find that information easily. That’s why I’m letting the discussion extend a little instead of settling for __class__ is, so I can hear the opinion of more experienced people. So far it’s been very fruitful!

I ran a small test to benchmark the performance of the 4 alternatives. The profiler results were:

               Python  PyPy (4x)
type()    is   2.138   2.594
__class__ is   2.185   2.437
type()    ==   2.213   2.625
__class__ ==   2.271   2.453

Unsurprisingly, is performed better than == for all cases. type() performed better in Python (2% faster) and __class__ performed better in PyPy (6% faster). Interesting to note that __class__ == performed better in PyPy than type() is.


Update 2: many people don’t seem to understand what I mean with “a class is a singleton”, so I’ll ilustrate with an example:

>>> class Foo(object): pass
...
>>> X = Foo
>>> class Foo(object): pass
...
>>> X == Foo
False
>>> isinstance(X(), Foo)
False
>>> isinstance(Foo(), X)
False

>>> x = type('Foo', (object,), dict())
>>> y = type('Foo', (object,), dict())
>>> x == y
False
>>> isinstance(x(), y)
False

>>> y = copy.copy(x)
>>> x == y
True
>>> x is y
True
>>> isinstance(x(), y)
True
>>> y = copy.deepcopy(x)
>>> x == y
True
>>> x is y
True
>>> isinstance(x(), y)
True

It doesn’t matter if there are N objects of type type, given an object, only one will be its class, hence it’s safe to compare for reference in this case. And since reference comparison will always be cheaper than value comparison, I wanted to know whether or not my assertion above holds. I’m reaching the conclusion that it does, unless someone presents evidence in contrary.

  • 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-30T22:35:58+00:00Added an answer on May 30, 2026 at 10:35 pm

    For old-style classes, there is a difference:

    >>> class X: pass
    ... 
    >>> type(X)
    <type 'classobj'>
    >>> X.__class__
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: class X has no attribute '__class__'
    >>> x = X()
    >>> x.__class__
    <class __main__.X at 0x171b5d50>
    >>> type(x)
    <type 'instance'>
    

    The point of new-style classes was to unify class and type. Technically speaking, __class__ is the only solution that will work both for new and old-style class instances, but it will also throw an exception on old-style class objects themselves. You can call type() on any object, but not every object has __class__. Also, you can muck with __class__ in a way you can’t muck with type().

    >>> class Z(object):
    ...     def __getattribute__(self, name):
    ...             return "ham"
    ... 
    >>> z = Z()
    >>> z.__class__
    'ham'
    >>> type(z)
    <class '__main__.Z'>
    

    Personally, I usually have an environment with new-style classes only, and as a matter of style prefer to use type() as I generally prefer built-in functions when they exist to using magic attributes. For example, I would also prefer bool(x) to x.__nonzero__().

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

Sidebar

Related Questions

How do I test whether an object is an instance of a particular class
I want to know whether the object that would be created from a class
I want to test whether an object is empty: {} . The following is
I want to test whether a certain string is contained in a short list
I want to test whether two languages have a string in common. Both of
More specifically I want to test whether Oracle ODP.Net is installed on a machine.
I have a very simple question. I want to test whether a particular port
Writing some test scripts in IronPython, I want to verify whether a window is
I have created a type like this: TypeBuilder tb = moduleBuilder.DefineType(myname, TypeAttributes.Class | TypeAttributes.Public,
The method I want to test has a local variable that references an object

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.