* in Python

Discussion in 'Python' started by placid, Jun 23, 2006.

  1. placid

    placid Guest

    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
     
    placid, Jun 23, 2006
    #1
    1. Advertising

  2. placid wrote:

    > 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


    see sections 4.7.2 and 4.7.3 in the tutorial:

    http://docs.python.org/tut/node6.html#SECTION006740000000000000000

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

    </F>
     
    Fredrik Lundh, Jun 23, 2006
    #2
    1. Advertising

  3. placid

    Duncan Booth Guest

    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
     
    Duncan Booth, Jun 23, 2006
    #3
  4. placid

    placid Guest

    Duncan Booth wrote:
    > 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


    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?
     
    placid, Jun 23, 2006
    #4
  5. placid wrote:
    > Duncan Booth wrote:
    >
    >>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

    >
    >
    > so * basically means that args is a list


    A tuple IIRC

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


    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 ''.split('@')])"
     
    Bruno Desthuilliers, Jun 23, 2006
    #5
  6. placid

    Duncan Booth Guest

    Bruno Desthuilliers wrote:

    >> so * basically means that args is a list

    >
    > A tuple IIRC


    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.
     
    Duncan Booth, Jun 23, 2006
    #6
  7. placid

    placid Guest

    Bruno Desthuilliers wrote:
    > placid wrote:
    > > Duncan Booth wrote:
    > >
    > >>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

    > >
    > >
    > > so * basically means that args is a list

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

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


    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(key)

    >
    >
    > --
    > bruno desthuilliers
    > python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    > p in ''.split('@')])"
     
    placid, Jun 23, 2006
    #7
  8. placid

    Duncan Booth Guest

    placid wrote:

    > 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(key)
    >


    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.
     
    Duncan Booth, Jun 23, 2006
    #8
  9. Duncan Booth wrote:
    > Bruno Desthuilliers wrote:
    >
    >
    >>>so * basically means that args is a list

    >>
    >>A tuple IIRC

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


    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 ''.split('@')])"
     
    Bruno Desthuilliers, Jun 23, 2006
    #9
  10. placid wrote:
    > Bruno Desthuilliers wrote:
    >

    (snip)
    >>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.

    >
    >
    > 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


    "doesn't work" is the most useless description of a problem.

    > def test(**args):
    > keys = args.keys()
    > for key in keys:
    > print key+"="+args(key)


    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.
    >>> def test(**kw):

    .... for item in kw.items():
    .... print "%s : %s" % item
    ....
    >>> test()
    >>> test(parrot="dead", walk="silly", nose="big")

    nose : big
    parrot : dead
    walk : silly
    >>> test(**{'answer':42})

    answer : 42
    >>>



    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    Bruno Desthuilliers, Jun 23, 2006
    #10
  11. placid

    placid Guest

    Bruno Desthuilliers wrote:
    > placid wrote:
    > > Bruno Desthuilliers wrote:
    > >

    > (snip)
    > >>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.

    > >
    > >
    > > 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

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

    >
    > you want args[key], not args(key)
    >
    > And you forget to tell how you called this code and what you got.


    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]
    !

    >
    > 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.
    > >>> def test(**kw):

    > ... for item in kw.items():
    > ... print "%s : %s" % item
    > ...
    > >>> test()
    > >>> test(parrot="dead", walk="silly", nose="big")

    > nose : big
    > parrot : dead
    > walk : silly
    > >>> test(**{'answer':42})

    > answer : 42
    > >>>


    Thanks for that now i know what * means.
     
    placid, Jun 24, 2006
    #11
    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. Replies:
    0
    Views:
    770
  2. Paul Moore
    Replies:
    0
    Views:
    654
    Paul Moore
    Mar 1, 2008
  3. Martin v. Löwis
    Replies:
    0
    Views:
    691
    Martin v. Löwis
    Mar 1, 2008
  4. Senthil Kumaran
    Replies:
    0
    Views:
    600
    Senthil Kumaran
    Jan 17, 2011
  5. R. David Murray
    Replies:
    0
    Views:
    792
    R. David Murray
    Jan 17, 2011
Loading...

Share This Page