* in Python

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

    #1

    * in Python

    Hi all,

    Can someone tell me what * in the following code means/does a Google
    search didnt turn up anything as i dont know what the * is called
    (related to Python and i dont think Python has pointers)

    def test(*args):
    pass

    and sometimes its;

    def test(**args):
    pass


    Cheers

  • Fredrik Lundh

    #2
    Re: * in Python

    placid wrote:
    [color=blue]
    > Can someone tell me what * in the following code means/does a Google
    > search didnt turn up anything as i dont know what the * is called[/color]

    see sections 4.7.2 and 4.7.3 in the tutorial:



    for more details, see the description of the "def" statement in the
    language reference.

    </F>

    Comment

    • Duncan Booth

      #3
      Re: * in Python

      placid wrote:
      [color=blue]
      > Hi all,
      >
      > Can someone tell me what * in the following code means/does a Google
      > search didnt turn up anything as i dont know what the * is called
      > (related to Python and i dont think Python has pointers)
      >[/color]
      * is for variable number of positional arguments, ** is for variable
      keyword arguments. The syntax is symmetrical when defining functions and
      when calling them.

      See http://docs.python.org/ref/calls.html
      and http://docs.python.org/ref/function.html

      Comment

      • placid

        #4
        Re: * in Python


        Duncan Booth wrote:[color=blue]
        > placid wrote:
        >[color=green]
        > > Hi all,
        > >
        > > Can someone tell me what * in the following code means/does a Google
        > > search didnt turn up anything as i dont know what the * is called
        > > (related to Python and i dont think Python has pointers)
        > >[/color]
        > * is for variable number of positional arguments, ** is for variable
        > keyword arguments. The syntax is symmetrical when defining functions and
        > when calling them.
        >
        > See http://docs.python.org/ref/calls.html
        > and http://docs.python.org/ref/function.html[/color]

        so * basically means that args is a list containing more arguments that
        can change in size, whereas ** means that args is a dictionary of
        key=value arguments?

        Comment

        • Bruno Desthuilliers

          #5
          Re: * in Python

          placid wrote:[color=blue]
          > Duncan Booth wrote:
          >[color=green]
          >>placid wrote:
          >>
          >>[color=darkred]
          >>>Hi all,
          >>>
          >>>Can someone tell me what * in the following code means/does a Google
          >>>search didnt turn up anything as i dont know what the * is called
          >>>(related to Python and i dont think Python has pointers)
          >>>[/color]
          >>
          >>* is for variable number of positional arguments, ** is for variable
          >>keyword arguments. The syntax is symmetrical when defining functions and
          >>when calling them.
          >>
          >>See http://docs.python.org/ref/calls.html
          >>and http://docs.python.org/ref/function.html[/color]
          >
          >
          > so * basically means that args is a list[/color]

          A tuple IIRC
          [color=blue]
          > containing more arguments that
          > can change in size, whereas ** means that args is a dictionary of
          > key=value arguments?
          >[/color]

          Why don't you try by yourself in the Python shell ? One of the nice
          things with Python is that it's quite easy to explore and experiment.


          --
          bruno desthuilliers
          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
          p in 'onurb@xiludom. gro'.split('@')])"

          Comment

          • Duncan Booth

            #6
            Re: * in Python

            Bruno Desthuilliers wrote:
            [color=blue][color=green]
            >> so * basically means that args is a list[/color]
            >
            > A tuple IIRC[/color]

            In a function definition * means that any remaining position arguments will
            be passed in as a tuple. In a function call the * means that any sequence
            will be unpacked as positional arguments: it doesn't have to be a list or
            a tuple.

            Comment

            • placid

              #7
              Re: * in Python


              Bruno Desthuilliers wrote:[color=blue]
              > placid wrote:[color=green]
              > > Duncan Booth wrote:
              > >[color=darkred]
              > >>placid wrote:
              > >>
              > >>
              > >>>Hi all,
              > >>>
              > >>>Can someone tell me what * in the following code means/does a Google
              > >>>search didnt turn up anything as i dont know what the * is called
              > >>>(related to Python and i dont think Python has pointers)
              > >>>
              > >>
              > >>* is for variable number of positional arguments, ** is for variable
              > >>keyword arguments. The syntax is symmetrical when defining functions and
              > >>when calling them.
              > >>
              > >>See http://docs.python.org/ref/calls.html
              > >>and http://docs.python.org/ref/function.html[/color]
              > >
              > >
              > > so * basically means that args is a list[/color]
              >
              > A tuple IIRC
              >[color=green]
              > > containing more arguments that
              > > can change in size, whereas ** means that args is a dictionary of
              > > key=value arguments?
              > >[/color]
              >
              > Why don't you try by yourself in the Python shell ? One of the nice
              > things with Python is that it's quite easy to explore and experiment.[/color]

              i did try it in a Python shell after i learnt what it was. Like i said
              *args will be a list, but when i try **args with the following code it
              doesnt work

              def test(**args):
              keys = args.keys()
              for key in keys:
              print key+"="+args(ke y)
              [color=blue]
              >
              >
              > --
              > bruno desthuilliers
              > python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
              > p in 'onurb@xiludom. gro'.split('@')])"[/color]

              Comment

              • Duncan Booth

                #8
                Re: * in Python

                placid wrote:
                [color=blue]
                > i did try it in a Python shell after i learnt what it was. Like i said
                > *args will be a list, but when i try **args with the following code it
                > doesnt work
                >
                > def test(**args):
                > keys = args.keys()
                > for key in keys:
                > print key+"="+args(ke y)
                >[/color]

                When you post here, it helps if instead of saying 'it doesnt work' you say
                what you expected to happen and what actually happened (and quote exact
                error messages, dont paraphrase them).

                If I try your code it works fine: it defines a function.

                If I try to call your function, even though you didn't include a call in
                what 'doesnt work', then I get the exception I would expect, namely that
                you are trying to call args as though it were a function 'args(key)'
                instead of subscripting it as a dictionary 'args[key]'. Fixing that will
                then generate a different error, but I'm sure you'll be able to figure it
                out.

                Comment

                • Bruno Desthuilliers

                  #9
                  Re: * in Python

                  Duncan Booth wrote:[color=blue]
                  > Bruno Desthuilliers wrote:
                  >
                  >[color=green][color=darkred]
                  >>>so * basically means that args is a list[/color]
                  >>
                  >>A tuple IIRC[/color]
                  >
                  >
                  > In a function definition * means that any remaining position arguments will
                  > be passed in as a tuple. In a function call the * means that any sequence
                  > will be unpacked as positional arguments: it doesn't have to be a list or
                  > a tuple.[/color]

                  yes, of course - I only saw it from the function's POV.

                  --
                  bruno desthuilliers
                  python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                  p in 'onurb@xiludom. gro'.split('@')])"

                  Comment

                  • Bruno Desthuilliers

                    #10
                    Re: * in Python

                    placid wrote:[color=blue]
                    > Bruno Desthuilliers wrote:
                    >[/color]
                    (snip)[color=blue][color=green]
                    >>Why don't you try by yourself in the Python shell ? One of the nice
                    >>things with Python is that it's quite easy to explore and experiment.[/color]
                    >
                    >
                    > i did try it in a Python shell after i learnt what it was. Like i said
                    > *args will be a list, but when i try **args with the following code it
                    > doesnt work[/color]

                    "doesn't work" is the most useless description of a problem.
                    [color=blue]
                    > def test(**args):
                    > keys = args.keys()
                    > for key in keys:
                    > print key+"="+args(ke y)[/color]

                    you want args[key], not args(key)

                    And you forget to tell how you called this code and what you got.

                    FWIW:
                    Python 2.4.3 (#1, Jun 3 2006, 17:26:11)
                    [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
                    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
                    >>> def test(**kw):[/color][/color][/color]
                    .... for item in kw.items():
                    .... print "%s : %s" % item
                    ....[color=blue][color=green][color=darkred]
                    >>> test()
                    >>> test(parrot="de ad", walk="silly", nose="big")[/color][/color][/color]
                    nose : big
                    parrot : dead
                    walk : silly[color=blue][color=green][color=darkred]
                    >>> test(**{'answer ':42})[/color][/color][/color]
                    answer : 42[color=blue][color=green][color=darkred]
                    >>>[/color][/color][/color]


                    --
                    bruno desthuilliers
                    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                    p in 'onurb@xiludom. gro'.split('@')])"

                    Comment

                    • placid

                      #11
                      Re: * in Python


                      Bruno Desthuilliers wrote:[color=blue]
                      > placid wrote:[color=green]
                      > > Bruno Desthuilliers wrote:
                      > >[/color]
                      > (snip)[color=green][color=darkred]
                      > >>Why don't you try by yourself in the Python shell ? One of the nice
                      > >>things with Python is that it's quite easy to explore and experiment.[/color]
                      > >
                      > >
                      > > i did try it in a Python shell after i learnt what it was. Like i said
                      > > *args will be a list, but when i try **args with the following code it
                      > > doesnt work[/color]
                      >
                      > "doesn't work" is the most useless description of a problem.
                      >[color=green]
                      > > def test(**args):
                      > > keys = args.keys()
                      > > for key in keys:
                      > > print key+"="+args(ke y)[/color]
                      >
                      > you want args[key], not args(key)
                      >
                      > And you forget to tell how you called this code and what you got.[/color]

                      Too much world cup means sleepless nights down here in Australia or i
                      would have seen the error with args(key) which suppose to be args[key]
                      !
                      [color=blue]
                      >
                      > FWIW:
                      > Python 2.4.3 (#1, Jun 3 2006, 17:26:11)
                      > [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
                      > Type "help", "copyright" , "credits" or "license" for more information.[color=green][color=darkred]
                      > >>> def test(**kw):[/color][/color]
                      > ... for item in kw.items():
                      > ... print "%s : %s" % item
                      > ...[color=green][color=darkred]
                      > >>> test()
                      > >>> test(parrot="de ad", walk="silly", nose="big")[/color][/color]
                      > nose : big
                      > parrot : dead
                      > walk : silly[color=green][color=darkred]
                      > >>> test(**{'answer ':42})[/color][/color]
                      > answer : 42[color=green][color=darkred]
                      > >>>[/color][/color][/color]

                      Thanks for that now i know what * means.

                      Comment

                      Working...