首页 > 解决方案 > 您如何访问由其他模块用户输入定义的变量?

问题描述

我正在编写一个程序: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是由 User 启动的程序,再次导入modulename1可能会导致不同的 user 。Turn 中的modulename2处理var1并定义attribute2然后var2再次在prog1中使用。有没有办法做到这一点?
input

条件

标签: pythonvariablesmodule

解决方案


推荐阅读