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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:21:51+00:00 2026-05-29T15:21:51+00:00

Say I have the following code in code.py: from foo import bar from foo2

  • 0

Say I have the following code in code.py:

from foo import bar
from foo2 import bar2
from foo3 import wrapper

class A(bar):
    def m1(self,x):
        return wrapper(x)

Then I have a reference to an A object in the var “obj” and I get the code for that object like so:

c = inspect.getsource(obj)

which returns:

class A(bar):
    pass

I need to be able to parse “inspect.getsource(obj)” to know that the A class depends on the “from foo import bar” and “from foo3 import wrapper” at the top of the file returned by “inspect.getsourcefile(obj)”.

How can I achieve this?

The only way to get close that I know of is by using modulefinder, but it runs on the whole file and would give me all three imports, not just the one used by the code of interest.

~~~~~~~~~~~~~~~~~~~~~~~~~~
Edit: updated definition
~~~~~~~~~~~~~~~~~~~~~~~~~~

  • 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-29T15:21:52+00:00Added an answer on May 29, 2026 at 3:21 pm

    If you want to check if an object or a class inherits from a class in another module, using inspect is probably the wrong way. You can instead use something like that:

    for base in A.__bases__:
      print "A depends on module %s" % base.__module__
    

    Or for an object:

    for base in (obj.__class__,) + obj.__class__.__bases__:
      print "obj depends on module %s" % base.__module__
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have the following html: <ul id=portfolio-list class=clearfix span-24> <li class=2012#foo bar
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test {
Let's say I have the following class: class Foo { public: Foo() { Bar();
Say I have the following code: class Parent { static string MyField = ParentField;
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Let's say we have the following code in a class: //class code TextBox t
Lets say I have the following code. double *return_array(void) { double foo[2]; foo[0] =
Let's say that I have the following code: SELECT * FROM table where company
Say I have the following Objective-C class: @interface Foo { int someNumber; NSString *someString;
Let's say I have following code: int f() { int foo = 0; int

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.