Home > Software engineering >  How to call functions as those?
How to call functions as those?

Time:02-10

I tried to download a video using this code;

from youtube_dl import YoutubeDL

def opts():
    url= "https://www.youtube.com/watch?v=ffcitRgiNDs"

    ydl_opts= {
        'format': '22'
    }
opts()

def download(url, ydl_opts):
    YoutubeDL(ydl_opts).download(url)
download()

And I got this error!

TypeError: download() missing 2 required positional arguments: 'url' and 'ydl_opts'

How to call fuctions like this. 'def download(url, ydl_opts)'. Can't call 'download()'

CodePudding user response:

You are defining local variables in opts that don't affect the outer scope. You could use global variables (not recommended) or return values from opts.

def opts():
    url= "https://www.youtube.com/watch?v=ffcitRgiNDs"

    ydl_opts= {
        'format': '22'
    }
    return url, ydl_opts

Now you can use

url, ydl_opts = opts()
download(url, ydl_opts)

or

download(*opts())

If you want download to take no parameters, call opts in download.

def download():
    url, ydl_opts = opts()
    YoutubeDL(ydl_opts).download(url)

CodePudding user response:

you can use Global variables

from youtube_dl import YoutubeDL

def opts():
    global url
    url= "https://www.youtube.com/watch?v=ffcitRgiNDs"

    global ydl_opts
    ydl_opts= {
        'format': '22'
    }
opts()

def download(url, ydl_opts):
    YoutubeDL(ydl_opts).download(url)
download(url,ydl_opts)
  • Related