简体   繁体   English

python将函数变量传递给另一个函数

[英]python passing function variables to another function

I am trying to pass a variable from one function into my main engine function in a separate file.我试图将一个函数中的变量传递到我的主引擎函数中的一个单独的文件中。 I have looked up other answers and gone through some but I can't seem to wrap my head around it..我已经查找了其他答案并经历了一些,但我似乎无法理解它..

load_file.py加载文件.py

def get_connection():
    cursor = connection.cursor()
    cursor.execute(
        "SELECT ID, Type, Server, Port, User, Password, isActive, FileExtension, FileContains, FileLocation, "
    "ScheduleMinutes, IntervalTime from DataConnection WHERE isActive=True")

    data_connection = cursor.fetchall()


def download_files(data_connection):
    for data_connection_detail in data_connection:
        connection_type = data_connection_detail[1]

        if connection_type == 'IMAP':
             ez_email.read_email_imap(data_connection_detail)

        elif connection_type == 'POP3':
             ez_email.read_email_pop3(data_connection_detail)

        elif connection_type == 'FTP':
             ez_ftp.easy_ftp(data_connection_detail)

main.py主文件

from load_file import download_files
from load_file import get_connection

def run_engine():
    while True:
        get_connection()
        download_files()


if __name__ == "__main__":
    run_engine()

When I pass 'data_connection' to 'download_files' function it says I have an unfilled parameter inside of my main.py engine.当我将“data_connection”传递给“download_files”函数时,它说我的 main.py 引擎中有一个未填充的参数。

I'm sorry if this has already been answered but I'm just having trouble understanding it.很抱歉,如果这已经得到回答,但我只是无法理解它。

The line线

def download_files(data_connection):

implies a parameter to be passed.表示要传递的参数。

I think you might want我想你可能想要

def run_engine(): while True: conn = get_connection() download_files(conn) def run_engine(): while True: conn = get_connection() download_files(conn)

The get_connection function looks strange to me, like it's not returning a connection but the results of fetchall . get_connection函数对我来说看起来很奇怪,就像它不是返回连接而是返回fetchall的结果。

Your download_files() function has a required parameter (data_connection).您的download_files()函数有一个必需参数 (data_connection)。 When you call it in run_engine, you are not passing a parameter.当您在 run_engine 中调用它时,您没有传递参数。

You will need to capture the returned variable from get_connection and pass it to download_files, like this:您需要从 get_connection 捕获返回的变量并将其传递给 download_files,如下所示:

data_con = get_connection()  # place the returned variable from get_connection into a variable called data_con
download_files(data_con)     # pass that data_con variable to the download_files function

But the returned value from get_connection is not actually a connection in your current code - you'll need to make sure you're returning/passing the right variable.但是 get_connection 的返回值实际上并不是当前代码中的连接 - 您需要确保返回/传递正确的变量。

load_file.py加载文件.py

def get_connection():
        cursor = connection.cursor()
        cursor.execute(
            "SELECT ID, Type, Server, Port, User, Password, isActive, FileExtension, FileContains, FileLocation, "
        "ScheduleMinutes, IntervalTime from DataConnection WHERE isActive=True")
    
        return cursor.fetchall()

def download_files(data_connection):

        for data_connection_detail in data_connection:
            # type email will be IMAP, POP3, or FTP
            connection_type = data_connection_detail[1]

            # create data_source object
            if connection_type == 'IMAP':
                ez_email.read_email_imap(data_connection_detail)

            elif connection_type == 'POP3':
                ez_email.read_email_pop3(data_connection_detail)

            elif connection_type == 'FTP':
                ez_ftp.easy_ftp(data_connection_detail)

main.py主文件

from load_file import get_connection
from load_file import download_files

def run_engine():
    while True:
        data = get_connection()
        download_files(data)



if __name__ == "__main__":
    run_engine()

The solution in this case is to return the object from the first function and use it as a parameter in the second.这种情况下的解决方案是从第一个函数返回对象,并在第二个函数中将其用作参数。

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

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