Oddity with function default arguments

S

Sheila King

Here is something that surprised me tonight:
print x
print y
print a
for item in mylist:
print item

2
5
6
7
8 1
2
A 1
2
3
4
5

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

Erik Max Francis

Sheila said:
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.
 
H

Heiko Wundram

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.
 

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,773
Messages
2,569,594
Members
45,113
Latest member
Vinay KumarNevatia
Top