开发者

Help with overriding python import

In application no.1 I have a settings.py file, and a utils.py, which contains the following:

from application_one import settings

def someFunction():
    // do some logic here based on imported settings

Then in application no.2 I do:

from application_one.utils import someFunction

In application no.2 I have a local settings.py and when I import 'someFunction()' I want to use the local settings.py not the file from application no.1. So how would one overide the i开发者_运维百科mport in application no.2?


You can do the following:

def someFunction(settings=settings):
    …  # Unmodified code ('settings' refers to the local 'settings' variable)

(this lets someFunction() use the Application 1 settings by default) and then call it from Application 2 by sending the local settings:

someFunction(application2_settings)  # Explicit settings sent by Application 2

One advantage of this approach is that your code in both Application 1 and 2 explicitly shows that someFunction() gives results that are setting-dependent.


Simply ensure that you import your settings after you have loaded the function you wish to overload.

However it seems that you'd be better of loading with namespaces intact, as this would prevent this entire issue from occurring.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜