python 3.5 - How to assign values returned from a function to another function -


i learning python. have come point functions, have loops can't call other functions within loop because otherwise duplicate results, want create function calls each function, gets data out of them , assigns them functions execute after , need data work, avoiding looping.

so let's have function :

def get_sound():     dirpath, directories, files in os.walk(xpath):         sound_file in files:             date = artist = album = title = ""             if sound_file.endswith('.flac'):                 print('flac file found, getting metadata , renaming...')                 flac_file = os.path.join(dirpath, sound_file)                 mutagen.flac import flac                 metadata = mutagen.flac.open(flac_file)                 (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) 

from function, got tuple of data. now, want create function :

def main():     get_sound()     find_key()     make_video() 

get_sound() return data, find_key() return data, , make_video() use both data fill variables , execute command them. data returned has no identifier, how pass get_sound() , find_key() returned data make_video ?

a function call (e.g. get_sound()) represents value function returns. can assign value variable , use value in subsequent operations , function calls:

def main():     sound = get_sound()     key = find_key()     make_video(sound, key) 

or can use functions in place of return values within operations , function calls:

def main():     make_video(get_sound(), find_key()) 

this assumes make_video takes 2 positional arguments, first can tuple returned get_sound. make_video might this:

def make_video(audio, key):     audio_destination_file, audio_name, audio_artist, audio_album, audio_title = audio     # audio_destination_file, audio_name, audio_artist,     # audio_album, audio_title , key ... 

if instead make_video function expects components of get_sound return value separate arguments, so:

def make_video(audio_destination_file, audio_name,                audio_artist, audio_album, audio_title, key):     # 

... either explicitly unpack them before call like x squared suggests or use splat operator unpacking when calling:

def main():     sound = get_sound()     key = find_key()     make_video(*sound, key) 

or

def main():     make_video(*get_sound(), find_key()) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -