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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:17:23+00:00 2026-05-23T14:17:23+00:00

I have a Python module that defines a classes and instantiates a number of

  • 0

I have a Python module that defines a classes and instantiates a number of instances of that classes. I would like to write a test that the correct number have been instantiated.

bar = Foo('abc')
baz = Foo('def')
...
quz = Foo('xyz')

Then in testing I have tried things like:

assert num = len([ x for x in dir(foo) if isinstance(x, foo.Foo)])    

which doesn’t work, because dir(foo) seems to give a list of Strings.

Is there a way to find the instances of a particular class in a module?

  • 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-23T14:17:24+00:00Added an answer on May 23, 2026 at 2:17 pm

    You’re really close. Use a double equals sign for comparison, and you need to use getattr() to retrieve the module member by name:

    assert num == len([getattr(foo, x) 
                       for x in dir(foo) 
                       if isinstance(getattr(foo, x), foo.Foo)])
    

    Might also be helpful to read the documentation on dir().

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

Sidebar

Related Questions

I have a python module that defines a number of classes: class A(object): def
I have a small web.py Python application that I would like to serve under
I have a python module that makes use of a huge dictionary global variable,
I have a python module that imports a module generated with swig. When I
I have created a Python module that creates and populates several SQLite tables. Now,
I have a Python program that uses the threading module. Once every second, my
I have a simple Python script that uses the socket module to send a
I have a Python script that uses built-in modules but also imports a number
I have a Python module installed on my system and I'd like to be
I have a series of 'tasks' that I would like to run in separate

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.