-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....

Brook
  • 189
  • 1
  • 1
  • 9
  • 5
    What's the actual problem you're trying to solve here? – jonrsharpe Nov 17 '16 at 08:05
  • Get the list of user only defined variable into a dictionnary and then manipulate the dictionnary after. – Brook Nov 17 '16 at 08:31
  • Why people put negative since this is a legitimate question. Maybe, people are unable to answer it ??? – Brook Nov 17 '16 at 08:32
  • 2
    No, that's the thing you're trying to do. *Why are you trying to do it?* It maybe be an http://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. – jonrsharpe Nov 17 '16 at 08:39
  • There is no answer to this question on Google and on StackOverFlow. Having the list of variables define by user (ie by program), allows to manipulate them in a programming way through other genetic functions. – Brook Nov 17 '16 at 08:44
  • 2
    http://stackoverflow.com/questions/10302119/list-user-defined-variables-python – Drako Nov 17 '16 at 08:44
  • Do you see my question ? By removing module, function names.... (removing user defined function, and user defined module too) This SO answer does not remove module/function names.... It's a globals() with built it in function remove. – Brook Nov 17 '16 at 08:47
  • 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. – Cristian Ciupitu Nov 17 '16 at 08:52
  • by the way I did not put negative, just elaborate on reasons, cause still not sure if you could not use answer from provided link to solve your task, and there few more similar problem solutions on SO, just try to go trough them or you are one of those who are looking for exact code answer for your solution and not able to use ideas to create your own solution? – Drako Nov 17 '16 at 08:55
  • ok, I re-phrased it. I tried and could not find solution, because this is not so trivial. – Brook Nov 17 '16 at 08:58
  • You need to clarify what do you mean by variable. In Python almost everything is first-class objects. Does `my_lambda = lambda x: x+1` is user defined variable? Is `g` an user defined variable when `def f(x): return x+2` and `g=f`? There are LOTS of details that you're not considering here. – Łukasz Rogalski Nov 17 '16 at 09:02
  • A 'variable' means variable which is neither a function, neither a module as indicated in the post.... (that's why, people dont read the post....) – Brook Nov 17 '16 at 09:06
  • Again, is `v = lambda: 42` a variable? Because it does hold a function. Technically there's little difference between that and `def v(): return 42`. Both declare a *symbol* which references a value, which in these cases is a function. It really is very very unclear what you're asking. I'm not saying that to be mean or anything, I just have no idea what you really want and hence cannot offer any appropriate solution. – deceze Nov 17 '16 at 09:16
  • @Brook, so you want to be able to access from `module1` the variables defined in `Main`? If so, create a third module and put there all the shared stuff. – Cristian Ciupitu Nov 17 '16 at 09:22
  • v() is a function since defined by def.... v = lambda:42 can be ambiguous since it is assigned to a lambda function. In the case of lambda, one can consider as a function. A variable which is neither a module neither a function is not unclear.... It's just because the guys putting negatives are unable to answer or provide decent analysis. – Brook Nov 17 '16 at 09:25
  • @cristian : cannot since I dont know the variable names in advance.... – Brook Nov 17 '16 at 09:30

1 Answers1

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
Community
  • 1
  • 1
Drako
  • 714
  • 9
  • 21
  • 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 – Brook Nov 17 '16 at 09:29
  • 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 – Drako Nov 17 '16 at 10:44