0

I've been using the globals()[name] method to look up Python variables by name. I can't use only an environment dict, because I need to be able to interface with existing scripts.

The problem is, it doesn't work with name = "op.eq", for example. Other than simply splitting on ., looking up op, then getting vars(globals()['op'])['eq'], is there a way to look up op.eq and other dot-notation references?

Leonora Tindall
  • 1,391
  • 2
  • 12
  • 30
  • This is why you shouldn't use `globals()`. Use a dictionary instead, as recommended in the linked duplicate. You may need to restructure your code entirely. – TigerhawkT3 Apr 10 '16 at 00:18
  • Unfortunately, I actually DO need `globals()` - I'm creating an interface to existing Python scripts, and I need to be able to access every global variable defined in those scripts. I have an env dict already, but I need to be able to "fall back" on accessing variables in the global scope if they aren't in the env dict. – Leonora Tindall Apr 10 '16 at 01:30
  • @TigerhawkT3 Can you please unmark this as duplicate? For the reasons I've outlined, I really do need to use `globals()`, and after looking at the documentation for a while all I can think of is writing my own dot reference resolver which I definitely do not want to do, since it's a core part of the language. – Leonora Tindall Apr 10 '16 at 02:40
  • @TigerhawkT3 Please either unmark this as duplicate or explain why it's still a duplicate even though my question is not the same as the question it's supposedly a duplicate of. – Leonora Tindall Apr 11 '16 at 20:50

0 Answers0