*args and **kwargs

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

    #1

    *args and **kwargs

    Ok, this is probably definitely a newbie question, but I have looked
    all over the Python library reference material and tutorials which I
    can find online and I cannot find a clear definition of what these are
    and more importantly how to use them. From what I can tell from their
    use in the examples I've seen, they are for passing a variable number
    of arguments to a function (which I need to do in a program I am
    working on). But how do you use them? Is there a fixed order in which
    the arguments within *arg or **kwarg should be passed or will be
    called within a function? I realize this probably involves a long-
    winded answer to a very simple and common programming problem, so if
    someone has a link to TFM, I'll gladly go RTFM. I just can't find it.

  • Diez B. Roggisch

    #2
    Re: *args and **kwargs

    JonathanB wrote:
    Ok, this is probably definitely a newbie question, but I have looked
    all over the Python library reference material and tutorials which I
    can find online and I cannot find a clear definition of what these are
    and more importantly how to use them. From what I can tell from their
    use in the examples I've seen, they are for passing a variable number
    of arguments to a function (which I need to do in a program I am
    working on). But how do you use them? Is there a fixed order in which
    the arguments within *arg or **kwarg should be passed or will be
    called within a function? I realize this probably involves a long-
    winded answer to a very simple and common programming problem, so if
    someone has a link to TFM, I'll gladly go RTFM. I just can't find it.
    That's because it's in the language reference, not in the library reference.



    Diez

    Comment

    • Thomas Jollans

      #3
      Re: *args and **kwargs

      "JonathanB" <doulos05@gmail .comwrote in message
      news:1181046478 .824231.117300@ q75g2000hsh.goo glegroups.com.. .
      Ok, this is probably definitely a newbie question, but I have looked
      all over the Python library reference material and tutorials which I
      can find online and I cannot find a clear definition of what these are
      and more importantly how to use them. From what I can tell from their
      use in the examples I've seen, they are for passing a variable number
      of arguments to a function (which I need to do in a program I am
      working on). But how do you use them? Is there a fixed order in which
      the arguments within *arg or **kwarg should be passed or will be
      called within a function? I realize this probably involves a long-
      winded answer to a very simple and common programming problem, so if
      someone has a link to TFM, I'll gladly go RTFM. I just can't find it.

      I hope this example code will help you understand:
      >>def a(*stuff):
      print repr(stuff)
      >>def b(**stuff):
      print repr(stuff)

      >>def c(*args, **kwargs):
      print 'args', repr(args)
      print 'kwargs', repr(kwargs)

      >>a(1,2,3)
      (1, 2, 3)
      >>b(hello='worl d', lingo='python')
      {'hello': 'world', 'lingo': 'python'}
      >>c(13,14,thene xt=16,afterthat =17)
      args (13, 14)
      kwargs {'afterthat': 17, 'thenext': 16}
      >>args = [1,2,3,4]
      >>kwargs = {'no-way': 23, 'yet-anotherInvalid. name': 24}
      >>c(*args, **kwargs)
      args (1, 2, 3, 4)
      kwargs {'no-way': 23, 'yet-anotherInvalid. name': 24}
      >>>

      (sorry for the messed-up formatting)


      Comment

      • Wildemar Wildenburger

        #4
        Re: *args and **kwargs

        JonathanB wrote:
        Ok, this is probably definitely a newbie question, but I have looked
        all over the Python library reference material and tutorials which I
        can find online and I cannot find a clear definition of what these are
        and more importantly how to use them. From what I can tell from their
        use in the examples I've seen, they are for passing a variable number
        of arguments to a function (which I need to do in a program I am
        working on). But how do you use them? Is there a fixed order in which
        the arguments within *arg or **kwarg should be passed or will be
        called within a function? I realize this probably involves a long-
        winded answer to a very simple and common programming problem, so if
        someone has a link to TFM, I'll gladly go RTFM. I just can't find it.
        >
        >
        Contents: Programming FAQ- General questions- Is there a source code-level debugger with breakpoints and single-stepping?, Are there tools to help find bugs or perform static analysis?, How can I c...

        (the first hit when you search python.org for *args and **kwargs)

        Basically 'args' is a tuple with all the positional arguments, kwargs is
        a dictionary with all the named arguments.
        Likewise you can pass a tuple to a function like func(*tuple), or a dict
        like func(**dictiona ry) or both, where the zuple has to come first.

        Comment

        • JonathanB

          #5
          Re: *args and **kwargs

          I hope this example code will help you understand:
          >>Code Snipped<<
          OOH!! That makes perfect sense, thanks!, *args are passed as a turple,
          **kwargs are passed as a dictionary. That means **kwargs is probably
          what I want.

          JonathanB


          Comment

          • BartlebyScrivener

            #6
            Re: *args and **kwargs

            On Jun 5, 7:31 am, "Diez B. Roggisch" <d...@nospam.we b.dewrote:
            JonathanB wrote:
            Ok, this is probably definitely a newbie question, but I have looked
            all over the Python library reference material and tutorials which I
            can find online and I cannot find a clear definition of what these are
            and more importantly how to use them.
            Also, well maybe op did not check THE tutorial. Or found explanation
            too terse. But it's there.



            rd




            Comment

            Working...