如何访问由其他模块用户输入定义的变量?

2024-09-28 05:20:06 发布

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

我正在编写一个程序:prog1,它正在导入另一个模块来定义一个变量。
它是这样工作的:

import modulename1
var1 = attribute1 #situated in modulename1
import modulename2
var2 = attribute2()
function2(var1):       # Imagine this being a api call with var1 part of url 
     #api call
     return response.text
function3(function2(var1)+ var2)  

模块1:

attribute1 = input('user value')

模块2:

def attribute2():
   from prog 1 import var 1
   atr2 = function(var1) + 'word'    #Imagine the function as an api call at 
   return atr2                       #this space

modulename1接受用户input定义变量attribute1。 现在,另一个模块:modulename2需要使用modulename1中的值导入var1 但是,它不能直接从modulename1导入,因为prog 1是用户启动的程序,再次导入modulename1可能会导致不同的用户^{
modulename2依次处理var1并定义attribute2
var2然后再次用于prog1。有办法吗

条件

  • 两个模块都必须完全导入

  • 出于显而易见的原因,modulename2不应导入modulename1


Tags: 模块用户import程序api定义callvar1

热门问题