Oddity with function default arguments

Discussion in 'Python' started by Sheila King, Jun 12, 2004.

  1. Sheila King

    Sheila King Guest

    Here is something that surprised me tonight:

    >>> def myfunc(x, y, a='A', *mylist):

    print x
    print y
    print a
    for item in mylist:
    print item


    >>> myfunc(2, 5, 6, 7, 8)

    2
    5
    6
    7
    8
    >>> myfunc(1, 2)

    1
    2
    A
    >>> myfunc(1, 2, 3, 4, 5)

    1
    2
    3
    4
    5
    >>> def myfunc(x, y, *mylist, a='A'):


    SyntaxError: invalid syntax

    Why isn't the default value for a printing out when I include list
    arguments?

    (This is Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on
    win32, if that makes a difference.)

    I would suspect at first a Python bug, but maybe I'm just not "seeing"
    something that I should?

    --
    Sheila King
    http://www.thinkspot.net/sheila/
    http://www.k12groups.org/
     
    Sheila King, Jun 12, 2004
    #1
    1. Advertising

  2. Sheila King wrote:

    > SyntaxError: invalid syntax
    >
    > Why isn't the default value for a printing out when I include list
    > arguments?


    The remaining arguments (*) or remaining keyword arguments (**) go at
    the end of the argument list.

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ Did you ever love somebody / Did you ever really care
    -- Cassandra Wilson
     
    Erik Max Francis, Jun 12, 2004
    #2
    1. Advertising

  3. Am Samstag, 12. Juni 2004 09:05 schrieb Sheila King:
    > Why isn't the default value for a printing out when I include list
    > arguments?


    You don't include list arguments. You just put positional arguments into a
    function call, and of course a positional argument at the position of the
    argument which has a default declared gets inserted there, that's why you
    don't see the A when you pass more than two arguments.

    All remaining positional arguments (once the named positional arguments have
    been filled) get put into the list *mylist, which is commonly called *args
    (it's not a list, btw., it's a tuple), which in your case are all arguments
    behind the third.

    Hope this makes this somewhat clearer...

    Heiko.
     
    Heiko Wundram, Jun 12, 2004
    #3
    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. Ajax Chelsea
    Replies:
    1
    Views:
    436
    Ron Natalie
    Dec 2, 2003
  2. Edward Diener
    Replies:
    14
    Views:
    4,984
    Josiah Carlson
    Apr 6, 2004
  3. Neo
    Replies:
    10
    Views:
    674
    sushant
    Jan 20, 2005
  4. tutmann
    Replies:
    4
    Views:
    440
  5. jmborr
    Replies:
    1
    Views:
    427
    Stargaming
    Nov 3, 2007
Loading...

Share This Page