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

2024-09-30 06:23:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经读过其他类似于oneone的文章,但是我没有得到答案。 我创建了一个具有以下结构的包:

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)

在我的\u other \u file.py的开头,我放了import语句:

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

在1函数中,我不能直接调用f1,我需要调用f1作为我的\file.f1(没关系)。 另一方面,of2函数是一个“shell”,用一个函数参数调用of3的函数,根据条件,该函数参数可以是my_file.f1或my_file.f2。换句话说,函数2的代码是:

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

上面的代码不起作用。但是,如果我使用下面的(没有我的\u文件),一切都可以:

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

为什么我需要调用同一个函数一次f1,另一次调用my\u file.f1?我做错什么了吗


Tags: 函数pyimportifinitmyresultfolder

热门问题