-3

This have been searched on Google and on Stack-Overflow, before asking the question, without success. Hence, this is not a trivial question...

I have two modules : Main and module1 and am doing this code in Main :

  import module1
  dict1= module1.getdict_userdefinedvariable()

dict1 is then manipulated in a programmatic way to check (assertion, validation,..).

However, if in getdict_userdefinedVariable(), I am using Globals() or Vars(), I have access ONLY to the variables defined in the sub-module.

Also, Globals(), Vars() or Ipython who_ls gives the list of all names including user-defined module names, user-defined function names.

So,How can we get variables defined in Main module and filter only user-defined variables by removing USER defined module, USER defined function names ?

There is

who_ls in ipython, 

but it contains module and function names....

16
  • 5
    What's the actual problem you're trying to solve here? Commented Nov 17, 2016 at 8:05
  • Get the list of user only defined variable into a dictionnary and then manipulate the dictionnary after. Commented Nov 17, 2016 at 8:31
  • 2
    No, that's the thing you're trying to do. Why are you trying to do it? It maybe be an xyproblem.info. Evidently several people disagree with your assessment of your question's legitimacy, but you've no evidence any of them can't answer it. Commented Nov 17, 2016 at 8:39
  • 2
    stackoverflow.com/questions/10302119/… Commented Nov 17, 2016 at 8:44
  • 2
    People are unable to answer it because it's not clear what you're asking. Rewrite your question properly. Give us more context, give us examples, rephrase some bits etc. Commented Nov 17, 2016 at 8:52

1 Answer 1

0

Does not this really work for You? I tried - got my vars listed: All credits to Niek de Klein, this is his answer from: List user defined variables, python If you don't put any underscores in front of your variables you could do:

#!/us/bin/python                                                                                    

foo1 = "Hello world"
foo2 = "bar"
foo3 = {"1":"a", "2":"b"}
foo4 = "1+1"

for name in dir():
    if not name.startswith('__'):
        myvalue = eval(name)
        print name, "is", type(myvalue), "and is equal to ", myvalue
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, 50%. Although function returning without argument might not be filtered.We can put a Try: Except too. If I want to use in sub-module, this does not catch the variables in main Thanks
to get main you have (i think 2 options) make its vars visible to submodul via shared resource module or (bad practise reverse import) or the best: just make it getvars(module) or duplicate method in main(may be you don't even need it in submodule) and get dict1 for main, dict2 for sub combain them and do whatever is needed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.