python 将函数变量传递给另一个函数
python passing function variables to another function
我试图将变量从一个函数传递到我在单独文件中的主引擎函数。我查阅了其他答案并浏览了一些,但我似乎无法理解它..
load_file.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()
当我将 'data_connection' 传递给 'download_files' 函数时,它说我的 main.py 引擎中有一个未填充的参数。
如果这个问题已经得到解答,我很抱歉,但我无法理解它。
行
def download_files(data_connection):
表示传递一个参数
我想你可能想要
def run_engine():
而真实的:
conn = get_connection()
download_files(conn)
get_connection
函数对我来说看起来很奇怪,好像它不是返回连接而是返回 fetchall
的结果。
您的 download_files()
函数有一个必需的参数 (data_connection)。当您在 run_engine 中调用它时,您没有传递参数。
您需要从 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
但是 get_connection 的返回值实际上并不是您当前代码中的连接 - 您需要确保 returning/passing 是正确的变量。
load_file.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()
这种情况下的解决方案是 return 来自第一个函数的对象,并将其用作第二个函数的参数。
我试图将变量从一个函数传递到我在单独文件中的主引擎函数。我查阅了其他答案并浏览了一些,但我似乎无法理解它..
load_file.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()
当我将 'data_connection' 传递给 'download_files' 函数时,它说我的 main.py 引擎中有一个未填充的参数。
如果这个问题已经得到解答,我很抱歉,但我无法理解它。
行
def download_files(data_connection):
表示传递一个参数
我想你可能想要
def run_engine(): 而真实的: conn = get_connection() download_files(conn)
get_connection
函数对我来说看起来很奇怪,好像它不是返回连接而是返回 fetchall
的结果。
您的 download_files()
函数有一个必需的参数 (data_connection)。当您在 run_engine 中调用它时,您没有传递参数。
您需要从 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
但是 get_connection 的返回值实际上并不是您当前代码中的连接 - 您需要确保 returning/passing 是正确的变量。
load_file.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()
这种情况下的解决方案是 return 来自第一个函数的对象,并将其用作第二个函数的参数。