简体   繁体   English

从导入模块中的父模块调用函数

[英]Call function from parent module in imported module

I have following problem: I have a .py file with some defined functions and I import a module. 我有以下问题:我有一个包含一些已定义函数的.py文件,并且导入了一个模块。 From this imported module I want do call a function from the parent module. 我想从这个导入的模块中调用父模块的函数。 How do I do this? 我该怎么做呢? I searched a lot but didn't found an answer. 我搜索了很多,但没有找到答案。 Here is some test code to show you my problem. 这是一些测试代码来向您展示我的问题。

File 1: 文件1:

from test2 import *

def one():
    print("one")
    pass

def two():
    print("two")
    print("now call function three from test one")

    three()


one()
two()

File 2: Imported as module 文件2:作为模块导入

def three():
    print("three")
    print("now call function one from test 1")

    one()

You may pass function as parameter: 您可以将函数作为参数传递:

Test 1: 测试1:

from test2 import *

def one():
    print("one")
    pass

def two():
    print("two")
    print("now call function three from test one")

    three(one)  # function one()


one()
two()

Test 2: 测试2:

def three(function):
    print("three")
    print("now call function one from test 1")

    function()

More pythonic solution (IMHO) is this: 更多pythonic解决方案(IMHO)是这样的:

File test1.py : 文件test1.py

import test2

def one():
    print("one")
    pass

def two():
    print("two")
    print("now call function three from test one")

    test2.three()

if __name__ == '__main__':
    one()
    two()

and test2.py : test2.py

import test1

def three():
    print("three")
    print("now call function one from test 1")

    test1.one()

then python3 test1.py produces: 然后python3 test1.py产生:

one
two
now call function three from test one
three
now call function one from test 1
one

Yes, there is a circular import. 是的,有一个循环导入。 Let's go step by step to see how it works: 让我们逐步看一下它是如何工作的:

  1. test1 is started as __main__ , because that's the special reserved name Python uses for starting programs. test1__main__开头,因为这是Python用于启动程序的特殊保留名称。
  2. test1 imports test2 in its first line, so the parsing continues there. test1在其第一行中导入了test2 ,因此解析在此处继续。
  3. test2 in turn imports test1 , that's why the first file is parsed again from the beginning, but now under its real name. test2依次导入test1 ,这就是为什么从头开始再次解析第一个文件,但现在以其真实名称对其进行解析的原因。 The import test2 is this time skipped, because test2 is already a known module. 这次import test2跳过import test2 ,因为test2已经是已知模块。 At the end of the file, the __name__ is not __main__ and the if condition is false. 在文件末尾, __name__不是__main__ ,并且if条件为false。 This is important! 这个很重要! Importing should not have any side effects. 导入不应有任何副作用。
  4. control is returned to test2 , the rest of file is parsed. 控件返回到test2 ,其余文件被解析。
  5. control is returned to test1 , the rest of file is parsed. 控件返回到test1 ,其余文件被解析。 The condition at the bottom of the file is satisfied this time, so the execution of the main part starts with one() 这次满足了文件底部的条件,因此主要部分的执行从one()开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM