What is the difference in *args, **kwargs vs calling with tuple and dict? [duplicate]

2024/11/15 6:35:30

This is a basic question. Is there a difference in doing

def foo(*args, **kwargs):"""standard function that accepts variable length."""# do somethingfoo(v1...vn, nv1=nv1...nvn=nvn)def foo(arg, kwargs):"""convention, call with tuple and dict."""# do somethingmytuple = (v1, ..vn)
mydict = {nv1=nv1, ...nvn=nvn}
foo(mytuple, mydict)

I could do the same thing with both, except that the later has a weird convention of creating a tuple and dictionary. But basically is there a difference? I can solve the same computational problem of handling infinite things because dict and tuple can take care of that for me anyway?

Is this more of an idiomatic part of Python i.e a good Syntactic Sugar for things that you do anyway? I.e function is going to handle this for you!

PS: Not sure of so many downvotes though I agree this is a copy of Why use packed *args/**kwargs instead of passing list/dict? and probably it should be corrected in the duplicate information. And that question has recieved upvotes. So am I being downvotes for not being able to find that?

Answer

args and kwargs are just names.
What really matters here is the * and **.

In your second example you can only call the function with 2 arguments, against the first example where you can call the function with basically infinite arguments.

https://en.xdnf.cn/q/119338.html

Related Q&A

Get result from multiprocessing process

I want to know if is there a way to make multiprocessing working in this code. What should I change or if there exist other function in multiprocessing that will allow me to do that operation.You can c…

How to do a second interpolation in python

I did my first interpolation with numpy.polyfit() and numpy.polyval() for 50 longitude values for a full satellite orbit.Now, I just want to look at a window of 0-4.5 degrees longitude and do a second …

How can I filter an ms-access databse, using QSqlTableModel and QLineEdit?

Im building a GUI that allows users to search information in a ms access database (yup. It has to be the ms access) The user has a textfield where he can type his search and the Tableview should update…

Python regex - Replace single quotes and brackets

Id like to replace quantities with name then a square bracket and a single quote with the contents inside. So, from this: RSQ(name[BAKD DK], name[A DKJ])to this:RSQ(BAKD DK, A DKJ)

Python unexpected EOF while parsing (python2.7)

Heres my python code. Could someone show me whats wrong with it? I try to learn an algorithm on solving 24 - point game. But I really dont know why this program has an error.from __future__ import div…

Call a function in repl without brackets

Would like to know if there is a way to call a function in python in repl just with the function name. $ python -i interace.py >>> load 834.png >>> sharpen >>> saverather tha…

how to work on a exist session in selenium with python?

I want to fill some field of a webpage and then send a request to it but this website has a very powerful login page to avoid sending requests for login from a robot so I cant log in with selenium bu…

how to find similarity between many strings and plot it

I have a xls file with one column and 10000 strings I want to do few things 1- make a heatmap or a cluster figure shows the similarity percentage between each string with another one.In order to find …

Python and Variable Scope

So I am recently new to Python, but I seem to be able to program some stuff and get it working. However Ive been trying to expand my knowledge of how things work in the language, and putting this simp…

How to check if element is orthogonally adjacent (next to) to existing elements?

Im trying to make a simple game where a building placed in a nested list must be next to another building. The problem I face is that if the building was placed at the sides, I cant use for loops to ch…