Python: Why Can't I Modify The Current Scope Within A Function Using Locals()?
Why does creating/modifying a member of locals() not work within a function? Python 2.5 (release25-maint, Jul 20 2008, 20:47:25) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]
Solution 1:
Why would it? It's designed to return a representation, and was never intended for editing the locals. It's not ever guaranteed to work as a tool for such, as the documentation warns.
Solution 2:
locals() return a copy of the namespace (which is the opposite of what globals() does). This means that any change you perform on the dictionary returned by locals() will have no effect. Check in dive into python at example 4.12.
Post a Comment for "Python: Why Can't I Modify The Current Scope Within A Function Using Locals()?"