Function params with **? what do these mean?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J Rice

    #1

    Function params with **? what do these mean?

    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

  • Dave Hansen

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

    On 20 Mar 2006 12:46:43 -0800 in comp.lang.pytho n, "J Rice"
    <rice.jeffrey@g mail.com> wrote:
    [color=blue]
    >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?[/color]

    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,to ny=45)

    See what happens. Should be mostly self-explanatory.

    Regards,
    -=Dave

    --
    Change is inevitable, progress is not.

    Comment

    • Jordan Greenberg

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

      in the parameter list, **param gets a dict of arguments that dont
      correspond to somthing in the formal parameter list.

      More & examples in the python docs:


      --
      Jordan T. Greenberg

      Comment

      • Larry Bates

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

        J Rice wrote:[color=blue]
        > 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
        >[/color]
        There are too forms that you may be confusing. First
        [color=blue][color=green][color=darkred]
        >>> def foo(x,y,z):
        >>> return x+y+z[/color][/color][/color]
        [color=blue][color=green][color=darkred]
        >>> t=[1,2,3][/color][/color][/color]
        [color=blue][color=green][color=darkred]
        >>> foo(*t)[/color][/color][/color]

        6


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

        The second is:
        [color=blue][color=green][color=darkred]
        >>> def foo(*args):
        >>> return sum(args)[/color][/color][/color]
        [color=blue][color=green][color=darkred]
        >>> foo(1,2,3)[/color][/color][/color]

        6

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

        or
        [color=blue][color=green][color=darkred]
        >>> def bar(**kwargs):
        >>> for key, value in kwargs.items():
        >>> print "key=%s, value=%s" % (key, value)
        >>> return[/color][/color][/color]
        [color=blue][color=green][color=darkred]
        >>> bar(a=1, b=2, c="this is a test")[/color][/color][/color]
        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


        Comment

        • J Rice

          #5
          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.

          Comment

          • Aahz

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

            In article <r56u12pb4ajvhs hvh40slb80di1bf eiagr@4ax.com>,
            Dave Hansen <iddw@hotmail.c om> wrote:[color=blue]
            >On 20 Mar 2006 12:46:43 -0800 in comp.lang.pytho n, "J Rice"
            ><rice.jeffrey@ gmail.com> wrote:[color=green]
            >>
            >>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?[/color]
            >
            >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[/color]

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

            "19. A language that doesn't affect the way you think about programming,
            is not worth knowing." --Alan Perlis

            Comment

            • Dave Hansen

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

              On 20 Mar 2006 15:45:36 -0800 in comp.lang.pytho n,
              aahz@pythoncraf t.com (Aahz) wrote:
              [color=blue]
              >In article <r56u12pb4ajvhs hvh40slb80di1bf eiagr@4ax.com>,
              >Dave Hansen <iddw@hotmail.c om> wrote:[/color]
              [...][color=blue][color=green]
              >>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[/color]
              >
              >Personally, I think it's a Good Idea to stick with the semi-standard
              >names of *args and **kwargs to make searching easier...[/color]

              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.

              Comment

              • Ben Cartwright

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

                Dave Hansen wrote:[color=blue]
                > On 20 Mar 2006 15:45:36 -0800 in comp.lang.pytho n,
                > aahz@pythoncraf t.com (Aahz) wrote:[color=green]
                > >Personally, I think it's a Good Idea to stick with the semi-standard
                > >names of *args and **kwargs to make searching easier...[/color]
                >
                > Agreed (though "kwargs" kinda makes my skin crawl).[/color]

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

                Fortunately, **kw is also semi-standard.

                --Ben

                Comment

                • Dave Benjamin

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

                  On Mon, 20 Mar 2006, Ben Cartwright wrote:
                  [color=blue]
                  > Dave Hansen wrote:[color=green]
                  >> On 20 Mar 2006 15:45:36 -0800 in comp.lang.pytho n,
                  >> aahz@pythoncraf t.com (Aahz) wrote:[color=darkred]
                  >>> Personally, I think it's a Good Idea to stick with the semi-standard
                  >>> names of *args and **kwargs to make searching easier...[/color]
                  >>
                  >> Agreed (though "kwargs" kinda makes my skin crawl).[/color]
                  >
                  > Coincidentally, "kwargs" is the sound my cat makes when coughing up a
                  > hairball.
                  >
                  > Fortunately, **kw is also semi-standard.[/color]

                  I prefer the semi-standard **kwds, myself. ;)

                  Comment

                  • Carl Banks

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

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

                    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

                    Comment

                    • Scott David Daniels

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

                      J Rice wrote:[color=blue]
                      > 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?[/color]

                      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.daniels@a cm.org

                      Comment

                      • Ben Finney

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

                        Scott David Daniels <scott.daniels@ acm.org> writes:
                        [color=blue]
                        > 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.[/color]

                        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

                        Comment

                        Working...