-1

I want to list variables "defined" in a module. And then I found there is no way to distinct variables defined in the module and variables imported from other modules. Is there any way to know if a variable is imported?

I KNOW inspect.getmembers and inspect.getmodule and dir but my concern is variable, not function or class definition.

AND I KNOW I COULD IMPORT MODULE RATHER THAN IMPORT VARIABLE FROM MODULE.

I just want to know is there a way or not :).

  • a.py, define a class

class A(object):
    pass
  • b.py, define a instance using class A
from a import A

ins_b = A()

  • c.py, define another instance using class A and
from a import A
from b import ins_b

ins_c = A()

I want to list variables like this:

["b.ins_b", "c.ins_c"]

but actually I could do is :

{
  <a.A instance at pos1>: ["b.ins_b", "c.ins_b"],
  <a.A instance at pos2>: ["c.ins_c"],
}

  • 1
    What are you trying to accomplish that needs this information? Ask about that. – kindall Jul 10 '19 at 03:33
  • gonna to create utils which record instance "path" to dynamic import. And the path should be ONLY ONE so that other utils, could use the path to maintain resource about the instance. – demo0w0 Jul 10 '19 at 03:42
  • You want to list all references to an object? – Klaus D. Jul 10 '19 at 03:48
  • In fact, I want to keep only one reference to an object. I know I could sort and take the first one. But I want to known if there is a way to keep the one which the object is created.@KlausD. – demo0w0 Jul 10 '19 at 03:55
  • when you do: `from a import A` then `A` *belongs* to the module you are importing, it essentially is equivalent to `import a; A = a.A; del a` you *don't import variables, you import modules* – juanpa.arrivillaga Jul 10 '19 at 04:16
  • ok, thank you very much to solve my question~@juanpa.arrivillaga – demo0w0 Jul 10 '19 at 10:50

1 Answers1

0

OK. Thanks to @juanpa.arrivillaga I know it is impossible to do this. And I just change the problem: how to know "the module which object is created" and "the attr name which the object is assigned".

I try to find something like "metaclass", maybe "metamodule" but I find it changes many behaviors which I do not need.

So according to "How to use inspect to get the caller's info from callee in Python?" and "Retrieve module object from stack frame", I do some trick to realize my idea.

import inspect
import re


attr_r = re.compile(u"\s*([^\s=]+)\s*=")


class A(object):
    def __init__(self):
        pre_frame = inspect.currentframe().f_back
        frame_info = inspect.getframeinfo(pre_frame)

        self.init_module_name = inspect.getmodule(pre_frame).__name__
        self.init_attr_name = ""
        if len(frame_info[3]) > 0:
            line = frame_info[3][0]
            m = attr_r.match(line)
            if m:
                self.init_attr_name = m.group(1)

the method to get attr name is trick but work for me.

# init_module_name == "a"
a = A()
print a.init_module_name, a.init_attr_name

# init_module_name == "a"
a = b = A()
print a.init_module_name, a.init_attr_name

# init_module_name == ""
a = \
   A()
print a.init_module_name, a.init_attr_name