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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:24:11+00:00 2026-05-23T11:24:11+00:00

I have a module foo that defines a class Foo , and instantiates a

  • 0

I have a module foo that defines a class Foo, and instantiates a number of instances of this class.

From other modules, I can import foo and obtain a list of the Foo objects instantiated by:

[getattr(foo,f) for f in dir(f) if isinstance(getattr(foo,f), foo.Foo)]

It would be convenient if I could do this from within the module foo. But as written, the name foo is meaningless inside foo, and changing to self doesn’t help.

Is there a way to use introspection within this module to find all instances of this class? I was hoping there was a way to avoid making a list and appending each instance.

  • 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-23T11:24:11+00:00Added an answer on May 23, 2026 at 11:24 am

    goal: “import foo and obtain a list of the Foo objects instantiated by…“

    You don’t need introspection at all. Imagine this is your class:

    class Registered(object):
        _all = set()
        def __init__(self):
            self.__class__._all.add(self)
    

    Demo; instantiate a bunch of objects and refer to them:

    >>> Registered()
    >>> Registered()
    >>> Registered()
    
    >>> Registered._all
    {<__main__.Registered object at 0xcd3210>, <__main__.Registered object at 0xcd3150>, <__main__.Registered object at 0xcd31d0>}
    

    Since you are using dir, if I understand you correctly, you are trying to find all the objects in the current module’s global variables. However it doesn’t really get you all the objects in the current module, only those bound to variables. If that’s what you really want, you can do {var for name,var in globals() if isinstance(var,Foo)}

    If that’s not what you really want, you can modify the above to keep track of the module the object was defined in via inspect.currentframe().f_back.f_globals or something, but this would prevent you from using factory functions defined in foo.py or in other modules.

    If you are actually trying to get all instances of something created in a module, this is a sign of a serious coding issue however; consider perhaps writing a factory function wrapper in that module, which does nothing except pass along the initialization args and return the result, keeping track of initialized objects.

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

Sidebar

Related Questions

I have a simple class that defines some constants, e.g.: module Foo class Bar
In python, If I have a class foo , I can call foo.__module__ to
I have a Perl script (foo.pl) that loads Foo.pm from the same directory using
In Ruby, how can I programmatically determine which class/module defines a method being called?
I have a GWT module with the X-GWT-Module-Base http://host:8080/foo/ and would like to call
I have module application. When I run it, the main window of that app
I have module that implements custom content type via NodeAPI hooks ( hook_insert ,
I have a module that sends an email to a specified email address but
i have a module (a single .py file, actually), with a class called HashedDir.
I have a module that reads the StandardError of a process. Everything works fine,

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.