Understanding def foo(*args)

S

sl33k_

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
 
R

rantingrick

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

Mel

sl33k_ said:
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.
 
C

Chris Rebert

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:.... print args
....(1, 2, 3)

If positional parameters precede the *-parameter, then they are
required and the *-parameter will receive any additional arguments:.... print 'a is', a
.... print 'b is', b
.... print 'args is', args
....Traceback (most recent call last):
a is 1
b is 2
args is ()a is 1
b is 2
args is (3,)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
 
U

Ulrich Eckhardt

sl33k_ said:
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
 
J

Jean-Michel Pichavant

sl33k_ said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top