如何从同一包中调用方法中的函数

时间:2018-07-24 10:00:19

标签: python module package

我已经读过其他one和其他one这样的线程,但是我找不到答案。 我创建了一个具有以下结构的包:

my_package (folder)
    __init__.py
    my_folder (folder)
        __init__.py
        my_file.py (contains f1 and f2 functions)
    my_other_folder (folder)
        __init__.py
        my_other_file.py (contains of1, of2, of3 functions)

在my_other_file.py的开头,我放置了导入语句:

from ..my_file import f1
from ..my_file import f2

在1函数内,我无法直接调用f1,我需要将f1称为my_file.f1(没关系)。 另一方面,of2函数是一个“外壳”,它使用一个函数参数调用of3函数,该函数参数取决于条件,可以是my_file.f1或my_file.f2。换句话说,of2的功能代码为:

def of2:
    if ....:
        result = of3(my_file.f1)
    if ....:
        result = of3(my_file.f2)
    return result

上面的代码不起作用。但是,如果我使用以下内容(没有my_file),则一切正常:

def of2:
    if ....:
        result = of3(f1)
    if ....:
        result = of3(f2)
    return result

为什么我需要一次调用f1并再次调用my_file.f1?我在做错什么吗?

0 个答案:

没有答案