How can I override a constant in an imported Python module?

后端 未结 4 794
再見小時候
再見小時候 2020-12-17 10:33

In my application I am using module within the package example called examplemod.

My app:

from example imp         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-17 10:57

    This is called monkey patching, and it's fairly common although not preferred if there's another way to accomplish the same thing:

    examplemod.config.CONSTANT = "Better value"
    

    The issue is that you're relying on the internals of examplemod and config remaining the same, so this could break if either module changes.

提交回复
热议问题