导入该模块时是否需要保留python模块的导入? [英] Do a python module's imports need to be carried over when importing that module?

查看:103
本文介绍了导入该模块时是否需要保留python模块的导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

太晚了,我对进口内(进口内)的进口感到困惑.对于这个问题,您将不得不忍受我的"apps"和"helpers",因为它们仅仅是解释我的python项目结构的工具.

It's late and I'm confused about imports within imports (within imports). For this question, you'll have to bear with my "apps" and "helpers" as they are merely vehicles for an explanation of my python project's structure.

假设python脚本app.py从app_helper.py导入了一个模块. app_helper.py的唯一要点是帮助app.py.它永远不会独立运行.

Suppose a python script app.py imports a module from app_helper.py. The only point of app_helper.py is to help app.py. It never runs on its own.

app.py

from app_helper import datechecker
import ...

date_val = 20140101-1010
print datechecker(date_val)

app_helper.py

app_helper.py

import ...

def datechecker(date_val):
    if (datetime.date.today().year - int(date_val[:4])) < 0:
        return "Error: Invalid year (YYYY) in %s. Too high." % date_val
    else:
        return True

import datetime是否需要位于app.py,app_helper.py或同时位于两者中?

Does import datetime need to be in app.py, app_helper.py, or both?

此外,假设我有app_helper_helper.py,它由app_helper导入.像app_helper一样,app_helper_helper永远不会单独执行,只是由app_helper调用,而app_helper只会由app.py调用.在这种情况下,app_helper.py中有一些我没有显示的东西.

Furthermore, suppose I've got app_helper_helper.py, which app_helper imports. Like app_helper, app_helper_helper is never executed on its own, its just called by app_helper, which is only ever called by app.py. In this case, there's something in app_helper.py that I didn't show.

app_helper.py

app_helper.py

from app_helper_helper import decision
import ...

def datechecker(date_val):
    if (datetime.date.today().year - int(date_val[:4])) < 0:
        return "Error: Invalid year (YYYY) in %s. Too high." % date_val
    else:
        return decision()

app_helper_helper.py

app_helper_helper.py

import ...

def decision():
    x = random.random()
    if x > .5:
        return True
    return False

那么进口去哪里了?

案例1的答案应该确定import random在这两个模块中的位置,但是app.py是否也需要import random?而import decision呢? app.py是否需要执行任何操作?还是app_helper.py可以解决这个问题?最重要的是,如果app.py还需要利用app_helper_helper的决策函数,那么app.py和app_helper.py都需要from app_helper_helper import decision还是仅仅是app.py?

The answer to Case 1 should determine where import random goes for these two modules, but does app.py need to import random as well? And how about import decision? Does app.py need to do any of that? Or does app_helper.py take care of it? On top of it all, if app.py also needs to make use of app_helper_helper's decision function, then do both app.py and app_helper.py require from app_helper_helper import decision or is it just app.py?

我知道 http://docs.python.org/2/tutorial/modules .html 应该涵盖这里的大部分基础,但是我想我只需要有人用我的术语或用app-app_helper-app_helper_helper术语来表达它即可.

I know the http://docs.python.org/2/tutorial/modules.html should cover most of the bases here, but I guess I just need someone to put it in my terms, or in app-app_helper-app_helper_helper terms.

推荐答案

如果表达式datetime.date仅出现在app_helper.py中,则唯一需要import datetime的模块是app_helper.py.

If the expression datetime.date appears only in app_helper.py, then the only module that has to import datetime is app_helper.py.

类似地,如果出现表达式random.random的唯一模块是app_helper_helper.py,则需要import random的唯一模块是app_helper_helper.py.

Similarly, if the only module that the expression random.random appears in is app_helper_helper.py, then the only module that needs to import random is app_helper_helper.py.

上级模块可能对下级模块要导入的内容不屑一顾.

Higher-level modules can live in blissful ignorance of what the lower-level modules are importing.

这篇关于导入该模块时是否需要保留python模块的导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆