Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Understanding def foo(*args)

Reply
Thread Tools

Understanding def foo(*args)

 
 
sl33k_
Guest
Posts: n/a
 
      01-30-2011
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
 
Reply With Quote
 
 
 
 
sl33k_
Guest
Posts: n/a
 
      01-30-2011
Sorry that parameter is **kwargs.
 
Reply With Quote
 
 
 
 
rantingrick
Guest
Posts: n/a
 
      01-30-2011
On Jan 30, 1:26*pm, sl33k_ <(E-Mail Removed)> 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?

 
Reply With Quote
 
Mel
Guest
Posts: n/a
 
      01-30-2011
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.

 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      01-30-2011
On Sun, Jan 30, 2011 at 11:26 AM, sl33k_ <(E-Mail Removed)> 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/cont...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/cont...word-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
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      01-30-2011
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

 
Reply With Quote
 
rusi
Guest
Posts: n/a
 
      01-31-2011
On Jan 31, 12:35*am, rantingrick <(E-Mail Removed)> 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...
 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      01-31-2011
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
Newbie: def must come before call to def? planetthoughtful Ruby 4 03-12-2007 11:36 AM
"def self.method" vs "class << self; def method" joevandyk@gmail.com Ruby 7 10-10-2006 08:46 AM
Is there a way to use "def self.new" to do the job of "def initialize"? Sean Ross Ruby 3 12-25-2003 04:59 AM
HttpModule -- how to intercept urls like http://localhost/abc/def or http://localhost/abc/def/ where abc, def are non virtual dir Jiong Feng ASP .Net 0 11-19-2003 05:29 AM



Advertisments