Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55903

Re: Understanding def foo(*args)

Path csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'defines': 0.05; 'interpreter': 0.05; 'args': 0.07; 'arguments.': 0.09; 'function:': 0.09; 'positional': 0.09; 'tuple': 0.09; '>>>': 0.11; 'am,': 0.13; 'wrote:': 0.14; 'def': 0.14; 'argument': 0.15; 'arguments': 0.15; 'traceback': 0.15; '(1,': 0.16; '(normally': 0.16; '**kwargs)': 0.16; '*args):': 0.16; 'arguments:': 0.16; 'class)': 0.16; 'function;': 0.16; 'precede': 0.16; 'subject:def': 0.16; 'cc:no real name:2**0': 0.17; '(most': 0.17; 'cheers,': 0.17; 'cc:2**0': 0.21; 'object': 0.22; 'jan': 0.22; 'last):': 0.23; 'header:In-Reply-To:1': 0.23; 'cc:addr:python-list': 0.24; 'parameters': 0.25; 'named': 0.26; 'function': 0.27; 'url:blog': 0.27; 'instead': 0.28; 'functions,': 0.28; 'least': 0.29; 'message-id:@mail.gmail.com': 0.29; "(i'm": 0.30; 'basically,': 0.30; 'grasp': 0.30; 'sun,': 0.30; 'typeerror:': 0.30; 'except': 0.30; 'cc:addr:python.org': 0.30; 'no.': 0.33; 'does': 0.33; 'file': 0.34; '"",': 0.34; 'hi,': 0.34; 'print': 0.35; 'url:docs': 0.35; 'but': 0.36; 'keyword': 0.36; 'used': 0.36; 'received:209.85': 0.38; 'next': 0.38; 'received:google.com': 0.38; 'i.e.': 0.38; 'url:org': 0.38; 'subject:: ': 0.39; 'url:python': 0.39; 'your': 0.61; 'receive': 0.68; 'concept': 0.70; '30,': 0.72; '11:26': 0.84; 'complexities': 0.84; 'friend!': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=7+AxkTzFSpPG3PslcOGVC+abUaJAa3g93ttbls26FpI=; b=UN803NF9s98YtNeEWzcqU3sd05MR6H3+crw3JIPCabtQimvu3F0S1DL9cvANYFqRTX tMZ1QHuWEMbA9+qSs/ZsMkZI1m7cv6w7TPphWXsbYH50mRcv2QuwoGbdRcHJxNx8WUXz BWn+EB70XiOrKGT0pjf1zO5aDmXHo9JdQpuuA=
DomainKey-Signature a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; b=OUeOCVd5rNWevXOQsXIK2Z7i3g3xNxs0gHSDSoivG/joY/UjcmpEMcKEBZV2q9JRwX 1xVUafkPWZzfe3b/4DKW74ZyUzMRr5gEZggKlfE6nCx+qAQezHss2QTl2sMLTaMIt4MC ogQ9ZtLvko99RFivSZIrb1tngRNEgecAIi3R0=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <17945aea-21b6-45b5-9c09-e602143f755e@k4g2000pre.googlegroups.com>
References <17945aea-21b6-45b5-9c09-e602143f755e@k4g2000pre.googlegroups.com>
Date Sun, 30 Jan 2011 11:54:38 -0800
X-Google-Sender-Auth ivNcDgLpKCR2YqoPRR30_kHJ-xE
Subject Re: Understanding def foo(*args)
From Chris Rebert <clp2@rebertia.com>
To sl33k_ <ahsanbagwan@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1473.1296417280.6505.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 82.94.164.166
X-Trace 1296417281 news.xs4all.nl 81475 [::ffff:82.94.164.166]:48143
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:55903

Show key headers only | View raw


On Sun, Jan 30, 2011 at 11:26 AM, sl33k_ <ahsanbagwan@gmail.com> 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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Understanding def foo(*args) Chris Rebert <clp2@rebertia.com> - 2011-01-30 11:54 -0800

csiph-web