Understanding def foo(*args)

Discussion in 'Python' started by sl33k_, Jan 30, 2011.

  1. sl33k_

    sl33k_ Guest

    Hi,

    I am struggling to grasp this concept about def foo(*args). Also, what
    is def bar(*args, *kwargs)?

    Isnt it like self must be the first parameter to the method/function?
    If not what are the exceptions?

    Also, can the terms method and function be used interchangeably?

    TIA
    sl33k_, Jan 30, 2011
    #1
    1. Advertising

  2. sl33k_

    sl33k_ Guest

    Sorry that parameter is **kwargs.
    sl33k_, Jan 30, 2011
    #2
    1. Advertising

  3. sl33k_

    rantingrick Guest

    On Jan 30, 1:26 pm, sl33k_ <> wrote:
    > Hi,
    >
    > I am struggling to grasp this concept about def foo(*args). Also, what
    > is def bar(*args, *kwargs)?


    FYI: the python intepretor is your friend!

    py> def foo(*args):
    print args

    py> foo(1)
    (1,)

    py> foo(1,2,3)
    (1, 2, 3)

    py> foo(1,[1,23], {'hat':'cat'})
    (1, [1, 23], {'hat': 'cat'})

    py> def bar(*args, **kw):
    print 'Args:', args
    print 'Kwds:', kw

    py> bar(1,2,3, hat='cat', spam='eggs')
    Args: (1, 2, 3)
    Kwds: {'hat': 'cat', 'spam': 'eggs'}


    > Isnt it like self must be the first parameter to the method/function?
    > If not what are the exceptions?


    Only *must* with methods!

    > Also, can the terms method and function be used interchangeably?


    Can the terms cars and truck be used interchangeably?
    rantingrick, Jan 30, 2011
    #3
  4. sl33k_

    Mel Guest

    sl33k_ wrote:

    > Hi,
    >
    > I am struggling to grasp this concept about def foo(*args). Also, what
    > is def bar(*args, *kwargs)?
    >
    > Isnt it like self must be the first parameter to the method/function?
    > If not what are the exceptions?
    >
    > Also, can the terms method and function be used interchangeably?
    >
    > TIA


    Try

    def foo (*args):
    print 'foo args:', repr (args)

    foo (1, 2, 3, 4)

    def bar (*args, **kwargs):
    print 'bar args:', args
    print 'bar kwargs:', kwargs

    bar (1, 2, 3, 4)
    bar (1, 2, 3, baz=6, boz=None)


    Mel.
    Mel, Jan 30, 2011
    #4
  5. sl33k_

    Chris Rebert Guest

    On Sun, Jan 30, 2011 at 11:26 AM, sl33k_ <> wrote:
    > Hi,
    >
    > I am struggling to grasp this concept about def foo(*args).


    The interactive interpreter is your friend! Try experimenting with it next time!

    http://docs.python.org/tutorial/controlflow.html#arbitrary-argument-lists
    That `def` defines a variadic function; i.e. a function which takes an
    arbitrary number of positional arguments.
    `args` will be a tuple of all the positional arguments passed to the function:
    >>> def foo(*args):

    .... print args
    ....
    >>> foo(1)

    (1,)
    >>> foo(1,2)

    (1, 2)
    >>> foo(1,2,3)

    (1, 2, 3)

    If positional parameters precede the *-parameter, then they are
    required and the *-parameter will receive any additional arguments:
    >>> def qux(a, b, *args):

    .... print 'a is', a
    .... print 'b is', b
    .... print 'args is', args
    ....
    >>> qux(1)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: qux() takes at least 2 arguments (1 given)
    >>> qux(1, 2)

    a is 1
    b is 2
    args is ()
    >>> qux(1, 2, 3)

    a is 1
    b is 2
    args is (3,)
    >>> qux(1, 2, 3, 4)

    a is 1
    b is 2
    args is (3, 4)

    > Also, what is def bar(*args, *kwargs)?


    You meant: def bar(*args, **kwargs)

    See http://docs.python.org/tutorial/controlflow.html#keyword-arguments
    Basically, the **-parameter is like the *-parameter, except for
    keyword arguments instead of positional arguments.

    > Also, can the terms method and function be used interchangeably?


    No. A method is function that is associated with an object (normally
    via a class) and takes this object as its first argument (typically
    named "self"). A function does not have any of these requirements.
    Thus, all method are functions, but the reverse is not true.
    (I'm ignoring complexities like classmethods and staticmethods for simplicity.)

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Jan 30, 2011
    #5
  6. sl33k_ wrote:
    > Isnt it like self must be the first parameter to the method/function?


    "self" is just customary as first parameter to memberfunctions, the
    language itself doesn't impose this convention, as e.g. C++ does with its
    "this".


    > Also, can the terms method and function be used interchangeably?


    This distinction doesn't exist in Python. You can put a reference to a
    "free" function as attribute in an object. You can store a reference to a
    "bound" memberfunction outside the object and call it.

    Objects and classes here are much more flexible than in C++ or Java.

    Uli
    Ulrich Eckhardt, Jan 30, 2011
    #6
  7. sl33k_

    rusi Guest

    On Jan 31, 12:35 am, rantingrick <> wrote:
    > > Also, can the terms method and function be used interchangeably?

    >
    > Can the terms cars and truck be used interchangeably?


    Oooff! A load of meaning in that one line -- I wonder though if the OP
    will understand...
    rusi, Jan 31, 2011
    #7
  8. sl33k_ wrote:
    > Hi,
    >
    > I am struggling to grasp this concept about def foo(*args). Also, what
    > is def bar(*args, *kwargs)?
    >
    > Isnt it like self must be the first parameter to the method/function?
    > If not what are the exceptions?
    >
    > Also, can the terms method and function be used interchangeably?
    >
    > TIA
    >

    "python *args **kwargs" in google.

    1st hit.

    JM
    Jean-Michel Pichavant, Jan 31, 2011
    #8
    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. Jiong Feng
    Replies:
    0
    Views:
    790
    Jiong Feng
    Nov 19, 2003
  2. Replies:
    3
    Views:
    371
    John Roth
    Jul 29, 2005
  3. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    377
    JaredGrubb
    Sep 24, 2008
  4. Gavin Kistner
    Replies:
    3
    Views:
    121
    Gavin Kistner
    Jan 29, 2004
  5. Replies:
    4
    Views:
    127
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page