如何将一个函数的返回值赋值给另一个函数
How to assign values returned from a function to another function
我在学习Python。我的重点是我的函数,有循环的函数不能从循环内调用其他函数,否则我会得到重复的结果,所以我想创建一个调用每个函数的函数,从中获取数据并分配它们到在之后执行并需要该数据才能工作的函数,避免循环。
假设我有这个功能:
def get_sound():
for dirpath, directories, files in os.walk(XPATH):
for sound_file in files:
date = artist = album = title = ""
if sound_file.endswith('.flac'):
print('Flac file found, getting metadata and renaming...')
flac_file = os.path.join(dirpath, sound_file)
from mutagen.flac import FLAC
metadata = mutagen.flac.Open(flac_file)
for (key, value) in metadata.items():
if key.startswith("date"):
date = value[0]
if key.startswith("artist"):
artist = value[0]
if key.startswith("album"):
album = value[0]
if key.startswith("title"):
title = value[0]
final_name = (date + " - " + artist +
" - " + album + " - " + title)
dest_file = os.path.join(dirpath, final_name)
os.renames(flac_file, dest_file)
return (dest_file, final_name, artist, album, title)
从那个函数中,我得到了一个数据元组。现在,我想做的是创建一个函数:
def main():
get_sound()
find_key()
make_video()
get_sound()
将return数据,find_key()
也将return数据,make_video()
将使用这两个数据来填充某些变量并执行命令跟他们。由于数据 returned 没有标识符,我如何将 get_sound()
和 find_key()
returned 数据传递给 make_video
?
一个函数调用(例如get_sound()
)表示函数return的值。您可以将该值赋给一个变量,并在后续操作和函数调用中使用该值:
def main():
sound = get_sound()
key = find_key()
make_video(sound, key)
或者您可以在操作和函数调用中使用函数代替它们的 return 值:
def main():
make_video(get_sound(), find_key())
这假设 make_video
接受两个位置参数,其中第一个可以是 return 由 get_sound
编辑的元组。所以 make_video
可能看起来有点像这样:
def make_video(audio, key):
audio_destination_file, audio_name, audio_artist, audio_album, audio_title = audio
# Do something with audio_destination_file, audio_name, audio_artist,
# audio_album, audio_title and key ...
如果您的 make_video
函数需要 get_sound
return 值的组件作为单独的参数,如下所示:
def make_video(audio_destination_file, audio_name,
audio_artist, audio_album, audio_title, key):
# Do something
... 然后在调用 之前显式解压它们,或者在调用时使用 splat 运算符解压:
def main():
sound = get_sound()
key = find_key()
make_video(*sound, key)
或
def main():
make_video(*get_sound(), find_key())
取决于 make_video()
s 参数的样子。如果它以一个元组作为参数,它的:
make_video(get_sound(), find_key())
或者,如果它采用单个参数,您可以使用 return 值分配多个变量,例如:
(dest_file, final_name, artist, album, title) = get_sound()
make_video(dest_file, final_name, artist, album, title, find_key())
我在学习Python。我的重点是我的函数,有循环的函数不能从循环内调用其他函数,否则我会得到重复的结果,所以我想创建一个调用每个函数的函数,从中获取数据并分配它们到在之后执行并需要该数据才能工作的函数,避免循环。
假设我有这个功能:
def get_sound():
for dirpath, directories, files in os.walk(XPATH):
for sound_file in files:
date = artist = album = title = ""
if sound_file.endswith('.flac'):
print('Flac file found, getting metadata and renaming...')
flac_file = os.path.join(dirpath, sound_file)
from mutagen.flac import FLAC
metadata = mutagen.flac.Open(flac_file)
for (key, value) in metadata.items():
if key.startswith("date"):
date = value[0]
if key.startswith("artist"):
artist = value[0]
if key.startswith("album"):
album = value[0]
if key.startswith("title"):
title = value[0]
final_name = (date + " - " + artist +
" - " + album + " - " + title)
dest_file = os.path.join(dirpath, final_name)
os.renames(flac_file, dest_file)
return (dest_file, final_name, artist, album, title)
从那个函数中,我得到了一个数据元组。现在,我想做的是创建一个函数:
def main():
get_sound()
find_key()
make_video()
get_sound()
将return数据,find_key()
也将return数据,make_video()
将使用这两个数据来填充某些变量并执行命令跟他们。由于数据 returned 没有标识符,我如何将 get_sound()
和 find_key()
returned 数据传递给 make_video
?
一个函数调用(例如get_sound()
)表示函数return的值。您可以将该值赋给一个变量,并在后续操作和函数调用中使用该值:
def main():
sound = get_sound()
key = find_key()
make_video(sound, key)
或者您可以在操作和函数调用中使用函数代替它们的 return 值:
def main():
make_video(get_sound(), find_key())
这假设 make_video
接受两个位置参数,其中第一个可以是 return 由 get_sound
编辑的元组。所以 make_video
可能看起来有点像这样:
def make_video(audio, key):
audio_destination_file, audio_name, audio_artist, audio_album, audio_title = audio
# Do something with audio_destination_file, audio_name, audio_artist,
# audio_album, audio_title and key ...
如果您的 make_video
函数需要 get_sound
return 值的组件作为单独的参数,如下所示:
def make_video(audio_destination_file, audio_name,
audio_artist, audio_album, audio_title, key):
# Do something
... 然后在调用
def main():
sound = get_sound()
key = find_key()
make_video(*sound, key)
或
def main():
make_video(*get_sound(), find_key())
取决于 make_video()
s 参数的样子。如果它以一个元组作为参数,它的:
make_video(get_sound(), find_key())
或者,如果它采用单个参数,您可以使用 return 值分配多个变量,例如:
(dest_file, final_name, artist, album, title) = get_sound()
make_video(dest_file, final_name, artist, album, title, find_key())