0

This is for a project I am working on to help my own workflow, and nothing that is for production. So I understand that what I am doing is probably definitely not the right thing to do, but I am curious if it is possible anyways.

I have some code like this:

A.py:

from B import *

def f1():
    ...

def f2():
    ...

...

Is there any way for module B, when imported, to get a list of the functions defined in the importer, A.py?

I thought about using the inspect module, to inspect the call stack. But I was unsure where the entry point would be, I assume it would be in the global scope of B.py. I am also unsure what the call stack looks like when importing a module.

Any help would be appreciated.

Thanks!

Community
  • 1
  • 1
Enrico Borba
  • 1,877
  • 2
  • 14
  • 26
  • 1
    `A` hasn't finished executing when `from B import *` is encountered so the best you could probably do is manual parsing of `A` in `B` and then trying to find function defs. That still won't guarantee that they are actually defined, for all you know there might be `del` statements there, dynamically created functions and god knows what else. – Dimitris Fasarakis Hilliard Mar 28 '17 at 22:54
  • Even though `A` hasn't finished executing, if `B` could find the path of the file that imported it, it can import that file and then get the function definitions. How could `B` find the path of the file that imported it? – Enrico Borba Mar 28 '17 at 23:09
  • Importing that module will get the existing reference to the incomplete module. – Ignacio Vazquez-Abrams Mar 28 '17 at 23:10
  • I see... Well, I wasn't super confident that what I was trying to do was possible. So, how would I go about finding what file imported `B`? If I can do that, that would already help immensely. – Enrico Borba Mar 28 '17 at 23:15
  • I don't think you'd even get an incomplete reference, pretty sure Python will detect the circular import and stop you in your tracks. – Dimitris Fasarakis Hilliard Mar 28 '17 at 23:18
  • 1
    @JimFasarakisHilliard: Python doesn't care what you import; it won't stop you unless you do something that would cause an error. – Ignacio Vazquez-Abrams Apr 03 '17 at 05:39

1 Answers1

0

If you know what module you are going to import B.py in then you could have something like this:

A.py

import B

def f1():
    return 1

def f2():
    return 1

def f3():
    return 1

if __name__=="__main__":
    print temp.allF()

B.py

import inspect, A

def allF():
    return inspect.getmembers(A, inspect.isfunction)

When you run A.py it will give you a list of functions. I'm not sure if there is a way to access the script which imports a given module, but if this is possible then you could change B.py slightly to use this.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Robbie
  • 4,672
  • 1
  • 19
  • 24
  • The issue is I don't know the name of the module that is importing `B.py`. :/ I have resorted to loading the functions from the main module using inspect, for now anyways. – Enrico Borba Mar 31 '17 at 18:46