Home > other >  Add additional argument to callable function for urlretrieve's 3rd parameter
Add additional argument to callable function for urlretrieve's 3rd parameter

Time:10-06

I have a function that downloads a file using the following code:

urllib.request.urlretrieve(url, filename, Handle_Progress)

Handle_Progress is the function that calculates the the progress percentage. The standard function receives 3 arguments:

Handle_Progress(blocknum, blocksize, totalsize)

For the purpose of my program I need to pass a fourth argument dlg.

I tried

urllib.request.urlretrieve(url, filename, Handle_Progress(dlg))

while making the function header

def Handle_Progress(dlg, blocknum, blocksize, totalsize):
``` but I get the following error: 

>TypeError: Handle_Progress() missing 3 required positional arguments: 'blocknum', 'blocksize', and 'totalsize'`

How can I pass a 4th parameter to the `Handle_Progress` function?

CodePudding user response:

Use a lambda

urllib.request.urlretrieve(url, filename, 
    lambda blocknum, blocksize, totalsize: Handle_Progress(dlg, blocknum, blocksize, totalsize)
)
  • Related