Override globals in function imported from another module

前端 未结 3 1355
孤独总比滥情好
孤独总比滥情好 2020-12-21 19:25

Let\'s say I have two modules:

a.py

value = 3
def x()
    return value

b.py

from          


        
3条回答
  •  甜味超标
    2020-12-21 19:39

    I had the same problem. But then I remembered eval was a thing.
    Here's a much shorter version(if you don't need arguments):
    b.py:

    from a import x as xx
    
    # Define globals for the function here
    glob = {'value': 4}
    def x():
        return eval(xx.__code__, glob)
    
    

    Hopefully after 2 years it'll still be helpful

提交回复
热议问题