-1

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

def foo(*args, **kwargs):
    """standard function that accepts variable length."""
    # do something

foo(v1...vn, nv1=nv1...nvn=nvn)

def foo(arg, kwargs):
    """convention, call with tuple and dict."""
    # do something

mytuple = (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?

Community
  • 1
  • 1
Nishant
  • 20,354
  • 18
  • 69
  • 101
  • 2
    You can *see the difference* in your snippets - the first version allows you to pass arbitrary positional and keyword arguments, the second requires you to pass precisely two arguments. – jonrsharpe May 09 '16 at 09:57
  • But with both I could solve my original problem of dealing with items of inifinte length. Tuple and Dicts already is infinitely large? – Nishant May 09 '16 at 09:59
  • 1
    http://stackoverflow.com/questions/33542959/why-use-packed-args-kwargs-instead-of-passing-list-dict?rq=1 – warvariuc May 09 '16 at 10:00
  • `**kwargs` are for named args, so `foo(v1...vn, nv1...nvn)` leaves it empty. You would need something like `foo(v1, ... vn, nv1=nv1, nv2=nv2...)` to involve it. Certainly it's just a syntactic sugar and you can pass arguments in simple pre-cooked tuples and dicts, but that wouldn't be convenient in many cases. – user3159253 May 09 '16 at 10:00
  • I agree with convinience part, but I don't see computationally how is this different to solve the original problem of infinite things. – Nishant May 09 '16 at 10:02
  • Probably its more of a copy of http://stackoverflow.com/questions/33542959/why-use-packed-args-kwargs-instead-of-passing-list-dict?rq=1 as pointed out by @warvariuc. – Nishant May 09 '16 at 10:05
  • 1
    What do you mean *"infinite length"*? Tuples and dictionaries can contain an arbitrary number of elements, but your RAM is fundamentally finite. You might want to look into generators and `itertools`, and/or explain what the actual problem you're trying to solve is. – jonrsharpe May 09 '16 at 10:32
  • @jonrsharpe To be precise both these approaches can help me handle arbitraty arguments. The second one seems more cleaner i.e a **Syntatic Sugar** and not something that can't be solved without a simple `tuple` vs `dict` right? It is only a beginner doubt as to why Python needs *args, **kwargs if it can be handled with simple things. I got downvoted for being a noob :) – Nishant May 09 '16 at 10:36
  • 1
    I would say that the second is *less* clean, and certainly less idiomatic, but YMMV. – jonrsharpe May 09 '16 at 10:41
  • 1
    I think you were downvoted because you didn't explain correctly your question. – warvariuc May 09 '16 at 13:05
  • 1
    Agree, I should have specifically asked if it was a **Syntactic Sugar** but I guess this is a general problem with me, I take time to formulate things. I guess spent more time and write is the only solution, will work on that. Thanks for explaining the reason! The same question had upvotes so it was like really depressing. – Nishant May 09 '16 at 13:07
  • 1
    Yes, I upvoted your question -- I understood it. – warvariuc May 09 '16 at 14:49

1 Answers1

1

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.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • But a convention of calling it with a tuple and dictionary actually solves the problem of me getting infinite items no? – Nishant May 09 '16 at 09:58
  • @Nishant: so would `foo(*sequence, **mapping)`. – Martijn Pieters May 09 '16 at 09:59
  • @Nishant No. There is no magic involved here. You should actually avoid the use of the name `tuple` as it shadows the built-in name. – DeepSpace May 09 '16 at 09:59
  • @DeepSpace: they are looking for the same syntax *when calling a function*. They want to pass in an arbitrary number of arguments. – Martijn Pieters May 09 '16 at 10:00
  • @MartijnPieters is there a fundamental difference in creating a tuple dict and calling it? Is def foo(*args, **kwargs): pass a convenient syntatic sugar? – Nishant May 09 '16 at 10:15