3 ms·
assignment is a statement if you want it to be, but you can generally use setattr() as well. E.g. modifying global scope as an expression: import sys modul
by emidln 14y ago
assignment is a statement if you want it to be, but you can generally use setattr() as well. E.g. modifying global scope as an expression:
import sys
module = sys.modules[__name__]
def foo(k,v):
setattr(module, k, v)
foo('spam','eggs')
assert(spam == 'eggs')
- masklinn 14y agoThat only "works" for the global scope, or by chance in some cases in the local scope if you do a star-import within the local scope (yeah…) I don't consider that working, it's a piling up of hacks. Which is great when you need them and they work, but... Also, there's a simpler way to implement your snippet: globals().__setitem__('spam','eggs')
- deleted 14y ago[deleted]