Reputation: 123
I have a function within a module that lists defined variables
Simplified Example:
for var_name in globals():
if not var_name.startswith('__'):
var_value = eval(var_name)
var_type = type(var_value)
print(var_name, "is", var_type, "and is equal to ", var_value)
This works when I run it from within the origin module, but not when the module is imported to another script. It only ever reads the variables defined in the origin module.
I also tried:
import __main__
for var_name in __main__.__dict__:
But that didn't work either. How can I get this function to work when imported into another script?
Thanks!
Upvotes: 0
Views: 72
Reputation: 389
You mean to use __main__.__dict__
, not __main__.dict
. That, with some minor loop modifications (i.e. eval
will not work as you want it to in this context, instead use __main__.__dict__[var_name]
) should get your code working.
Upvotes: 1