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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:59:54+00:00 2026-06-17T23:59:54+00:00

Need a way to check if an object is an instance of any class

  • 0

Need a way to check if an object is an instance of any class in some particular module.

I know I can do it by explicitly importing every class from that module and checking with a tuple:

from my_module import ClassOne, ClassTwo

>>> isinstance(my_obj, (ClassOne, ClassTwo))
True

But in reality, the module I’m importing from has a TON of classes in it, and seems needlessly verbose to import them all explicitly, use them to build a huge tuple, and type check against it. I’ve tried a few things to avoid this:

import my_module

# Test my_obj against the module itself
>>> isinstance(my_obj, my_module)
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

# Try to test against a wildcard attribute on my_module
>>> isinstance(my_obj, my_module.*)
SyntaxError: invalid syntax

#Try to build a tuple of clases with iteration to check against
>>> for klass in my_module:
TypeError: 'module' object is not iterable

Is there a way to type check against ALL the classes in my_module, without explicitly naming them in a tuple?

Optional Background Info:
I may be overlooking a better way to approach my problem — if you are wondering, here’s the situation:

We are exporting data from a Google App Engine app to an app we have hosted on Rackspace. We are serializing the data with pickle and then sending it over to our Rackspace server with HTTP Requests.

Some of the data in the Google App Engine database is of GAE-specific data-types, imported from google.appengine.api.datastore_types. If any of these data types go across the wire to our Rackspace server, they will raise an error depickling, because our Rackspace app doesn’t have the required GAE libraries. So, on the way out of GAE, I’m checking to see if any of the outgoing objects have a type from google.appengine.api.datastore_types. If they do, I either convert them to a builtin data-type or I delete the field off the object.

  • 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-17T23:59:56+00:00Added an answer on June 17, 2026 at 11:59 pm

    You can use inspect.getmembers to get all the classes in your module:

    inspect.getmembers(my_module,inspect.isclass)
    

    This will return a list of name-class pairs. You just want the classes:

    my_module_classes = tuple(x[1] for x in inspect.getmembers(my_module,inspect.isclass))
    

    One thing that I managed to overlook when I initially wrote this answer is the ability to check a class’s __module__ attribute. You might be able to get away with checking if the __module__ is what you expect it to be:

    from somewhere import module
    
    if getattr(obj, '__module__', None) == module.__name__:
        # obj is from module.
    

    This is likely to be cheaper than isinstance checking against a large list of class names.

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

Sidebar

Related Questions

I need to check in some fast way if there is any text nodes
I need a way to check if an object is an instance of another
Is there a simple way to check the type of an object? I need
I need to create some unit tests that confirm that instance of a class
I need to check whether a certain class extends or implements a particular interface.
Is there any way to efficiently check if the variable is Object or Array,
Is there any way in which you can test whether an instance of a
I need an simple way to check whether a string that is sent to
I need to find a way to check if the mouse moves in c#,
I basically need to check if there is an easier way to do this

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.