Namespace iteration

I have a namespace similar to this:

Namespace (aTQ = No, bE = No, bEQ = No, b = No, bQ = No, c = No, c = None, cJ = None, d = None, g = None, jR = ['xx', '015'], lC = None, l = None)

How can I iterate over it so that I can find and replace the value β€œxx” for the key β€œjR”?

+7
source share
1 answer

I'm not sure what you know in advance (the name jR or only one name has the value not None ), but you can try using vars (), which, like __dict__ , should be a dict that has names in your namespace as its keys . So, if you have the string 'jR' somewhere,

 vars()['jR'] = vars()['jR'][0] 

Similarly, you can get the same dict using namespace.__dict__ instead of vars()

will leave you the first value in ['xx','015'] as the only value for jR .

EDIT: To be clear, since vars() and __dict__ both return dicts, you can iterate through them:

 for k in namespace.__dict__: if namespace.__dict__[k] is not None: <<do something>> 
+8
source

All Articles