Function params with **? what do these mean?

Discussion in 'Python' started by J Rice, Mar 20, 2006.

  1. J Rice

    J Rice Guest

    I'm sorry for such a basic question, but I haven't been able to phrase
    a search that gets me an answer and my books are totally silent on
    this. I have seen a number of python function defs that take
    parameters of the form (**param1). Looks like a pointer... but my
    books on python (basic as they are) don't make a mention. What is
    this?

    Jeff
    J Rice, Mar 20, 2006
    #1
    1. Advertising

  2. J Rice

    Dave Hansen Guest

    On 20 Mar 2006 12:46:43 -0800 in comp.lang.python, "J Rice"
    <> wrote:

    >I'm sorry for such a basic question, but I haven't been able to phrase
    >a search that gets me an answer and my books are totally silent on
    >this. I have seen a number of python function defs that take
    >parameters of the form (**param1). Looks like a pointer... but my
    >books on python (basic as they are) don't make a mention. What is
    >this?


    It's a way of accepting a varying number of named arguments. In the
    function, the parameter becomes a dictionary with parameter names as
    the keys corresponding to the passed parameter values.

    It's harder to explain than understand. Try playing with the
    following function in the python interpreter:

    def test(a,b='b', *c, **d):
    print a,b,c,d

    A couple suggestions for tests:

    test(1,2,3,4)
    test(a=1,b=2,c=3,d=4)
    test(2,4,6,8,10,12,ralph=23,tony=45)

    See what happens. Should be mostly self-explanatory.

    Regards,
    -=Dave

    --
    Change is inevitable, progress is not.
    Dave Hansen, Mar 20, 2006
    #2
    1. Advertising

  3. Jordan Greenberg, Mar 20, 2006
    #3
  4. J Rice

    Larry Bates Guest

    J Rice wrote:
    > I'm sorry for such a basic question, but I haven't been able to phrase
    > a search that gets me an answer and my books are totally silent on
    > this. I have seen a number of python function defs that take
    > parameters of the form (**param1). Looks like a pointer... but my
    > books on python (basic as they are) don't make a mention. What is
    > this?
    >
    > Jeff
    >

    There are too forms that you may be confusing. First

    >>> def foo(x,y,z):
    >>> return x+y+z


    >>> t=[1,2,3]


    >>> foo(*t)


    6


    This tells python to expand t and pass it as as 3
    separate arguments

    The second is:

    >>> def foo(*args):
    >>> return sum(args)


    >>> foo(1,2,3)


    6

    This allows you to treat all the arguments to a function
    as a list of arguments no matter how many there are.

    or

    >>> def bar(**kwargs):
    >>> for key, value in kwargs.items():
    >>> print "key=%s, value=%s" % (key, value)
    >>> return


    >>> bar(a=1, b=2, c="this is a test")

    key=a, value=1
    key=c, value=this is a test
    key=b, value=2


    This allows you to access keyword arguments via a dictionary
    in the function, instead of individually.


    You can combine these to make very powerful functions/methods
    that can handle different numbers of arguments and keyword
    arguments.

    def foo(*args, **kwargs):
    ....


    Larry Bates
    Larry Bates, Mar 20, 2006
    #4
  5. J Rice

    J Rice Guest

    Re: Function params with **? what do these mean?

    Wow, this is incredibly useful! I can understand why an introductory
    book wouldn't make use of them, but I am really glad to know about
    them. I can think of a bunch of ways to simply some code I have using
    this.
    J Rice, Mar 20, 2006
    #5
  6. J Rice

    Aahz Guest

    In article <>,
    Dave Hansen <> wrote:
    >On 20 Mar 2006 12:46:43 -0800 in comp.lang.python, "J Rice"
    ><> wrote:
    >>
    >>I'm sorry for such a basic question, but I haven't been able to phrase
    >>a search that gets me an answer and my books are totally silent on
    >>this. I have seen a number of python function defs that take
    >>parameters of the form (**param1). Looks like a pointer... but my
    >>books on python (basic as they are) don't make a mention. What is
    >>this?

    >
    >It's a way of accepting a varying number of named arguments. In the
    >function, the parameter becomes a dictionary with parameter names as
    >the keys corresponding to the passed parameter values.
    >
    >It's harder to explain than understand. Try playing with the
    >following function in the python interpreter:
    >
    > def test(a,b='b', *c, **d):
    > print a,b,c,d


    Personally, I think it's a Good Idea to stick with the semi-standard
    names of *args and **kwargs to make searching easier...
    --
    Aahz () <*> http://www.pythoncraft.com/

    "19. A language that doesn't affect the way you think about programming,
    is not worth knowing." --Alan Perlis
    Aahz, Mar 20, 2006
    #6
  7. J Rice

    Dave Hansen Guest

    On 20 Mar 2006 15:45:36 -0800 in comp.lang.python,
    (Aahz) wrote:

    >In article <>,
    >Dave Hansen <> wrote:

    [...]
    >>It's harder to explain than understand. Try playing with the
    >>following function in the python interpreter:
    >>
    >> def test(a,b='b', *c, **d):
    >> print a,b,c,d

    >
    >Personally, I think it's a Good Idea to stick with the semi-standard
    >names of *args and **kwargs to make searching easier...


    Agreed (though "kwargs" kinda makes my skin crawl). I don't use these
    features often in my code, but when I do, I follow the convention. The
    example was just for illustrative purposes, and the names chosen for
    easy typing.

    It is important to note that using "args" and "kwargs" is a convention
    rather than a requirement, analogous to "self". You can use different
    identifiers, but future maintainers of your code will be annoyed.

    But it won't affect the operation of the code. I found the test case
    "test(a=1,b=2,c=3,d=4)" to be most edifying.

    Regards,
    -=Dave

    --
    Change is inevitable, progress is not.
    Dave Hansen, Mar 21, 2006
    #7
  8. Re: Function params with **? what do these mean?

    Dave Hansen wrote:
    > On 20 Mar 2006 15:45:36 -0800 in comp.lang.python,
    > (Aahz) wrote:
    > >Personally, I think it's a Good Idea to stick with the semi-standard
    > >names of *args and **kwargs to make searching easier...

    >
    > Agreed (though "kwargs" kinda makes my skin crawl).


    Coincidentally, "kwargs" is the sound my cat makes when coughing up a
    hairball.

    Fortunately, **kw is also semi-standard.

    --Ben
    Ben Cartwright, Mar 21, 2006
    #8
  9. Re: Function params with **? what do these mean?

    On Mon, 20 Mar 2006, Ben Cartwright wrote:

    > Dave Hansen wrote:
    >> On 20 Mar 2006 15:45:36 -0800 in comp.lang.python,
    >> (Aahz) wrote:
    >>> Personally, I think it's a Good Idea to stick with the semi-standard
    >>> names of *args and **kwargs to make searching easier...

    >>
    >> Agreed (though "kwargs" kinda makes my skin crawl).

    >
    > Coincidentally, "kwargs" is the sound my cat makes when coughing up a
    > hairball.
    >
    > Fortunately, **kw is also semi-standard.


    I prefer the semi-standard **kwds, myself. ;)
    Dave Benjamin, Mar 21, 2006
    #9
  10. J Rice

    Carl Banks Guest

    Re: Function params with **? what do these mean?

    Aahz wrote:
    > Personally, I think it's a Good Idea to stick with the semi-standard
    > names of *args and **kwargs to make searching easier...


    I usually do stick to these names (since the I usually only use them
    when forwarding arguments to another function, where such names are a
    pretty good description), but I can't think of any particular reason to
    search for all occurrences of them.

    Carl Banks
    Carl Banks, Mar 21, 2006
    #10
  11. J Rice wrote:
    > I'm sorry for such a basic question, but I haven't been able to phrase
    > a search that gets me an answer and my books are totally silent on
    > this. I have seen a number of python function defs that take
    > parameters of the form (**param1). Looks like a pointer... but my
    > books on python (basic as they are) don't make a mention. What is
    > this?


    At the risk of being thought of as beating a dead horse, this was a
    _great_ way to ask this question. Others may note that nobody told
    you you were abusing the newsgroup to do your work for you; you told
    us how you tried to answer the question for yourself.

    So, anyhow, thanks for taking the time to write your question properly.

    --
    -Scott David Daniels
    Scott David Daniels, Mar 23, 2006
    #11
  12. J Rice

    Ben Finney Guest

    Scott David Daniels <> writes:

    > At the risk of being thought of as beating a dead horse, this was a
    > _great_ way to ask this question. [...]
    > So, anyhow, thanks for taking the time to write your question properly.


    Take that risk, please. There's enough lambasting of (and probably
    much more private grumbling over) poor delivery of questions, but not
    enough praise when *good* form is followed.

    The latter does much more to show what we consider appropriate.

    --
    \ "Ice Water? Get some onions - that'll make your eyes water!" |
    `\ -- Groucho Marx |
    _o__) |
    Ben Finney
    Ben Finney, Mar 23, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Walter Dnes (delete the 'z' to get my real address

    What do these warnings mean?

    Walter Dnes (delete the 'z' to get my real address, Jun 5, 2004, in forum: C Programming
    Replies:
    15
    Views:
    604
    Dan Pop
    Jun 10, 2004
  2. Replies:
    0
    Views:
    282
  3. AAaron123

    What does Request.Params("dir") mean

    AAaron123, Feb 2, 2009, in forum: ASP .Net
    Replies:
    14
    Views:
    856
    AAaron123
    Feb 12, 2009
  4. C Barrington-Leigh
    Replies:
    1
    Views:
    1,178
    Tim Leslie
    Sep 10, 2010
  5. Barry
    Replies:
    9
    Views:
    429
    Ara.T.Howard
    Sep 15, 2005
Loading...

Share This Page