How to modify the local namespace in python

前端 未结 7 527
爱一瞬间的悲伤
爱一瞬间的悲伤 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:27

    I think you could solve the problem tackling it from a completely different point.
    Functions are object, with their dictionaries; therefore, you can add g to f, and use it:

    def g():
       print "g"
    
    def f():
        f.g()
    
    f.g = g
    

提交回复
热议问题