0

I'm trying to set a variable based on dynamic input from a dictionary. However, I'm not trying to set an attribute based on string input, I'm trying to set a variable. So, instead of:

this.attribute = True

I'm trying to dynamically set:

this = True

I can't create any conditional statements to predetermine what this might be, and it comes entirely from a string in a dictionary. Is there a way I can do this?

Craig
  • 563
  • 1
  • 6
  • 18
  • the closest thing is `locals()['this'] = True;`, but it's not recommended... if you have control over it, change it to an object or dictionary. – Corley Brigman Jul 17 '18 at 14:17
  • 1
    You generally do *not* need to do this; you'd use a dictionary or list instead. Python's global module namespace is a dictionary too, reachable via `globals()`. – Martijn Pieters Jul 17 '18 at 14:18
  • 2
    @CorleyBrigman: locals in a function are not mutable through `locals()` as that produces only a *one way reflection* of the local namespace; only `globals()` produces a direct reference to the global namespace. (`locals()` outside of a function gives you the same object `globals()` gives you). – Martijn Pieters Jul 17 '18 at 14:20

0 Answers0