How to modify the local namespace in python

前端 未结 7 509
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 19:39

How can I modify the local namespace of a function in python? I know that locals() returns the local namespace of the function when called inside it, but I want to do somet

相关标签:
7条回答
  • 2020-12-01 20:41

    This seems to work

    def add_to_locals(l):
        l['newlocal'] = 1
    
    add_to_locals(locals())
    assert newlocal
    
    0 讨论(0)
提交回复
热议问题