Override module method where from…import is used

前端 未结 1 1683
甜味超标
甜味超标 2020-11-30 03:29

I have problem to override method where from...import statement is used. Some example to illustrate the problem:

# a.py module
def print_message(msg):
    pr         


        
1条回答
  •  無奈伤痛
    2020-11-30 03:52

    With your a and b modules untouched you could try implementing c as follows:

    import a
    
    def _new_print_message(message):
        print "NEW:", message
    
    a.print_message = _new_print_message
    
    import b
    b.execute()
    

    You have to first import a, then override the function and then import b so that it would use the a module that is already imported (and changed).

    0 讨论(0)
提交回复
热议问题