简体   繁体   English

将函数内部的变量传递给另一个Python文件

[英]Passing a variable inside of a function to another Python file

I have a two python files which are classification_model.py and result.py . 我有两个python文件,分别是classification_model.pyresult.py There is a function in classification_model.py file as classification_model.py文件中有一个函数

def learningbased():
    ann_accuracy = (metrics.accuracy_score(expected, predicted))

I want to get this ann_accuracy variable into result.py file.I used 我想把这个ann_accuracy变量放到result.py文件中。我用过

from classification_model import *
from classification_model import learningbased

But it doesn't work. 但它不起作用。 Can anyone tell me how can I do this. 谁能告诉我怎么能这样做呢。

good way: You can return the value of ann_accuracy in the function and call the function in result.py to get the value. 好方法:你可以在函数中返回ann_accuracy的值,并调用result.py中的函数来获取值。

#classification_model.py:
def learningbased():
    ann_accuracy = (metrics.accuracy_score(expected, predicted))
    return ann_accuracy

#result.py
from classification_model import *
ann_accuracy = learningbased()

alternate way (use when desparate, no other solution): declare ann_accuracy as a global variable in classification_model.py. 替代方式(在绝望时使用,没有其他解决方案):将ann_accuracy声明为classification_model.py中的全局变量。 That way when you do from classification_model import * and call the function, you'll get the global variable in result.py 这样当你from classification_model import *调用函数时,你将获得result.py中的全局变量

I think you should declare your project as a package, by adding a __init__.py file : 我认为您应该通过添加__init__.py文件将项目声明为包:

project_folder
    |_ __init__.py
    |_ classification_model.py
    |_ result.py

After doing that, in result.py you can do : 完成后,在result.py你可以这样做:

from . import classification_model
classification_model.learning_based()

It's a little different from what was already suggested and it enables you to have a clear organisation of your project. 它与已经建议的内容略有不同,它使您能够清楚地组织项目。

Declare ann_accuracy as a global variable in classification_model.py. 将ann_accuracy声明为classification_model.py中的全局变量。 Use from classification_model import * , you can use/access the global variable in result.py. 使用from classification_model import * ,您可以在result.py中使用/访问全局变量。

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

相关问题 如何从另一个文件导入 function 中的数值变量 - Python - How to import a numerical variable inside a function from another file - Python 将在一个变量中创建的列表传递给类中的另一个变量(PYTHON) - Passing a list created in one variable to another variable inside a class (PYTHON) 将 function 的文档字符串传递给另一个文件 python3 - Passing docstring of a function to another file python3 将 function 变量传递给另一个文件 python - passing function variables to another file python 如何使用 function 访问 python 文件中另一个 python 文件中的变量 - how to access the variable from other python file in another python file inside a function using tkinter 我试图在另一个文件中使用另一个 python 文件,而我的 function 没有传递数字 - Im trying to use another python file inside a different one and my function isnt passing numbers 在Python中引用另一个function里面的一个变量 - Refer to a variable inside another function in Python 通过将参数作为变量传递来从另一个python文件执行python文件 - Execute python file from another python file by passing argument as variable Python通过[0]在函数中传递变量 - Python passing variable in function with [0] Python:在传递给另一个函数之前先替换变量值 - Python: Substitute variable values before passing to another function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM