Connecting Tech Pros Worldwide Forums | Help | Site Map

Difference between default arguments and keyword arguments

Edward Diener
Guest
 
Posts: n/a
#1: Jul 18 '05
In the tutorial on functions there are sections on default arguments and
keyword arguments, yet I don't see the syntactic difference between them.
For default arguments the tutorial shows:

def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):

while for keyword arguments the tutorial shows:

def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):

The syntax 'keyword = value' is used for both as far as I can see. How does
one distinguish between them or are they both part of the same combined
concept, which is: if one calls the function with less than the required
number of arguments but does specify keyword values, those values are used,
else the defaults are supplied. Or is there really a syntactic difference
between default arguments and keyword arguments which I have missed above ?



DoubleM
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Difference between default arguments and keyword arguments


Edward Diener wrote:[color=blue]
> In the tutorial on functions there are sections on default arguments and
> keyword arguments, yet I don't see the syntactic difference between them.
> For default arguments the tutorial shows:
>
> def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
>
> while for keyword arguments the tutorial shows:
>
> def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
>
> The syntax 'keyword = value' is used for both as far as I can see. How does
> one distinguish between them or are they both part of the same combined
> concept, which is: if one calls the function with less than the required
> number of arguments but does specify keyword values, those values are used,
> else the defaults are supplied. Or is there really a syntactic difference
> between default arguments and keyword arguments which I have missed above ?
>
>[/color]
All arguments are keyword arguments. They may or may not have a default
value. In your example, voltage is a keyword argument, but it has no
default.

Consider the following:[color=blue][color=green][color=darkred]
>>> def fn(a,b):[/color][/color][/color]
print 'a = ',a
print 'b = ',b

[color=blue][color=green][color=darkred]
>>> fn(b=1,a=2)[/color][/color][/color]
a = 2
b = 1[color=blue][color=green][color=darkred]
>>> fn(1,2)[/color][/color][/color]
a = 1
b = 2[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
Hope this helps,
Mike
Edward Diener
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Difference between default arguments and keyword arguments


DoubleM wrote:[color=blue]
> Edward Diener wrote:[color=green]
>> In the tutorial on functions there are sections on default arguments
>> and keyword arguments, yet I don't see the syntactic difference
>> between them. For default arguments the tutorial shows:
>>
>> def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
>>
>> while for keyword arguments the tutorial shows:
>>
>> def parrot(voltage, state='a stiff', action='voom', type='Norwegian
>> Blue'):
>>
>> The syntax 'keyword = value' is used for both as far as I can see.
>> How does one distinguish between them or are they both part of the
>> same combined concept, which is: if one calls the function with less
>> than the required number of arguments but does specify keyword
>> values, those values are used, else the defaults are supplied. Or is
>> there really a syntactic difference between default arguments and
>> keyword arguments which I have missed above ?
>>
>>[/color]
> All arguments are keyword arguments. They may or may not have a
> default value. In your example, voltage is a keyword argument, but
> it has no default.
>
> Consider the following:[color=green][color=darkred]
> >>> def fn(a,b):[/color][/color]
> print 'a = ',a
> print 'b = ',b
>
>[color=green][color=darkred]
> >>> fn(b=1,a=2)[/color][/color]
> a = 2
> b = 1[color=green][color=darkred]
> >>> fn(1,2)[/color][/color]
> a = 1
> b = 2[/color]

Makes sense. Thanks ! The tutorial should explain it more clearly.


Marco Herrn
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Difference between default arguments and keyword arguments


On 2004-04-04, DoubleM <DoublemPI@netscape.net> wrote:[color=blue]
> All arguments are keyword arguments. They may or may not have a default
> value. In your example, voltage is a keyword argument, but it has no
> default.[/color]

But what does this mean?:
[color=blue][color=green][color=darkred]
>>> __import__(name="eggs")[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __import__() takes no keyword arguments


Is it because the other arguments are optional? The function is defined
as follows:

__import__( name[, globals[, locals[, fromlist]]])

Marco
--
Marco Herrn herrn@gmx.net
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

Paul Prescod
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Difference between default arguments and keyword arguments


Marco Herrn wrote:
[color=blue]
> On 2004-04-04, DoubleM <DoublemPI@netscape.net> wrote:
>[color=green]
>>All arguments are keyword arguments. They may or may not have a default
>>value. In your example, voltage is a keyword argument, but it has no
>>default.[/color]
>
>
> But what does this mean?:
>[color=green][color=darkred]
> >>> __import__(name="eggs")[/color][/color]
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: __import__() takes no keyword arguments[/color]

Functions implemented in C do not always have the property that every
argument is a keyword argument. Functions implemented in Python do have
that property. It's an implementation quirk.

Paul Prescod

Paul Prescod
Guest
 
Posts: n/a
#6: Jul 18 '05

re: Difference between default arguments and keyword arguments


Marco Herrn wrote:
[color=blue]
> On 2004-04-04, DoubleM <DoublemPI@netscape.net> wrote:
>[color=green]
>>All arguments are keyword arguments. They may or may not have a default
>>value. In your example, voltage is a keyword argument, but it has no
>>default.[/color]
>
>
> But what does this mean?:
>[color=green][color=darkred]
> >>> __import__(name="eggs")[/color][/color]
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: __import__() takes no keyword arguments[/color]

Functions implemented in C do not always have the property that every
argument is a keyword argument. Functions implemented in Python do have
that property. It's an implementation quirk.

Paul Prescod



Scott David Daniels
Guest
 
Posts: n/a
#7: Jul 18 '05

re: Difference between default arguments and keyword arguments


Edward Diener wrote:[color=blue]
> ... Makes sense. Thanks ! The tutorial should explain it more clearly.[/color]

Right now you know what is confusing about the tutorial. _Please_ take
the time to propose a fix to the tutorial -- you have an "impertise" (as
opposed to "expertise") that tells you confusing interpretations of the
text of the tutorial. Once you just know" this stuff, you won't be able
to know what is confusing. So the time you spend, _right_now_ is the
most valuable contribution you can make to Python for a while. Help
us improve our documents.

--
-Scott David Daniels
Scott.Daniels@Acm.Org
John Roth
Guest
 
Posts: n/a
#8: Jul 18 '05

re: Difference between default arguments and keyword arguments



"Edward Diener" <eldiener@earthlink.net> wrote in message
news:UmTbc.9904$NL4.4809@newsread3.news.atl.earthl ink.net...[color=blue]
> In the tutorial on functions there are sections on default arguments and
> keyword arguments, yet I don't see the syntactic difference between them.
> For default arguments the tutorial shows:
>
> def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
>
> while for keyword arguments the tutorial shows:
>
> def parrot(voltage, state='a stiff', action='voom', type='Norwegian[/color]
Blue'):[color=blue]
>
> The syntax 'keyword = value' is used for both as far as I can see. How[/color]
does[color=blue]
> one distinguish between them or are they both part of the same combined
> concept, which is: if one calls the function with less than the required
> number of arguments but does specify keyword values, those values are[/color]
used,[color=blue]
> else the defaults are supplied. Or is there really a syntactic difference
> between default arguments and keyword arguments which I have missed above[/color]
?

The difference (from the tutorial) is that default arguements apply to
the function *definition*, while keyword arguements apply to the
function *call*. It's a subtle difference that does seem to be quite
easy to miss.

In going through the tutorial I also noticed that it explained
the * notation, but not the ** notation. I think this also needs
to be repaired - if one is in the tutorial, both should be.

John Roth


Roy Smith
Guest
 
Posts: n/a
#9: Jul 18 '05

re: Difference between default arguments and keyword arguments


"John Roth" <newsgroups@jhrothjr.com> wrote:[color=blue]
> In going through the tutorial I also noticed that it explained
> the * notation, but not the ** notation. I think this also needs
> to be repaired - if one is in the tutorial, both should be.[/color]

I suspect this will be an unpopular opinion, but I don't think either
belong in the tutorial.

I think the function of a tutorial is to introduce somebody to the main
features of the language, not cover every nook and cranny of the syntax.
The * and ** syntaxes (synti?), while certainly useful, are also
somewhat advanced topics. I think their appearance in an introductory
document is misplaced.
Edward Diener
Guest
 
Posts: n/a
#10: Jul 18 '05

re: Difference between default arguments and keyword arguments


Roy Smith wrote:[color=blue]
> "John Roth" <newsgroups@jhrothjr.com> wrote:[color=green]
>> In going through the tutorial I also noticed that it explained
>> the * notation, but not the ** notation. I think this also needs
>> to be repaired - if one is in the tutorial, both should be.[/color]
>
> I suspect this will be an unpopular opinion, but I don't think either
> belong in the tutorial.
>
> I think the function of a tutorial is to introduce somebody to the
> main features of the language, not cover every nook and cranny of the
> syntax. The * and ** syntaxes (synti?), while certainly useful, are
> also somewhat advanced topics. I think their appearance in an
> introductory document is misplaced.[/color]

Along the same lines, I would like to add to that the explanation for
classes in the tutorial is very poor since it assumes a much higher
understanding of Python than a tutorial should about the language. A much
simpler and more direct explanation regarding classes in Python would be
much better. While I mostly got it because I have programmed extensively in
other OOP languages, I would expect your average Python beginner to be
completely lost by much of the high-level explanation in that chapter.
Whoever wrote it was much more interested in explaining the theory of
classes in Python, to beginners no less !, than they were toward explaining
what classes are in Python and how to use them. And why iterators and
generators are included in that chapter are beyond me. Also the tutorial
mentions some usages of classes, in previous sections, before the
explanation of classes occur, which I feel is definitely a mistake.


John Roth
Guest
 
Posts: n/a
#11: Jul 18 '05

re: Difference between default arguments and keyword arguments



"Edward Diener" <eldiener@earthlink.net> wrote in message
news:yD1cc.12064$yN6.3263@newsread2.news.atl.earth link.net...[color=blue]
> Roy Smith wrote:[color=green]
> > "John Roth" <newsgroups@jhrothjr.com> wrote:[color=darkred]
> >> In going through the tutorial I also noticed that it explained
> >> the * notation, but not the ** notation. I think this also needs
> >> to be repaired - if one is in the tutorial, both should be.[/color]
> >
> > I suspect this will be an unpopular opinion, but I don't think either
> > belong in the tutorial.[/color][/color]

I certainly wouldn't object to removing them; that's why I said "if".
[color=blue][color=green]
> > I think the function of a tutorial is to introduce somebody to the
> > main features of the language, not cover every nook and cranny of the
> > syntax. The * and ** syntaxes (synti?), while certainly useful, are
> > also somewhat advanced topics. I think their appearance in an
> > introductory document is misplaced.[/color]
>
> Along the same lines, I would like to add to that the explanation for
> classes in the tutorial is very poor since it assumes a much higher
> understanding of Python than a tutorial should about the language. A much
> simpler and more direct explanation regarding classes in Python would be
> much better. While I mostly got it because I have programmed extensively[/color]
in[color=blue]
> other OOP languages, I would expect your average Python beginner to be
> completely lost by much of the high-level explanation in that chapter.
> Whoever wrote it was much more interested in explaining the theory of
> classes in Python, to beginners no less !, than they were toward[/color]
explaining[color=blue]
> what classes are in Python and how to use them. And why iterators and
> generators are included in that chapter are beyond me. Also the tutorial
> mentions some usages of classes, in previous sections, before the
> explanation of classes occur, which I feel is definitely a mistake.[/color]

One of the things going on here is that the tutorial has grown over
the releases; it's not entirely clear what level of expertise is expected
for a reader. As you say, there are topics that are fairly advanced.
On the other hand, since the libraries are shipped in source, and since
they do use all of those features, I think they should be mentioned in
some kind of tuorial.

John Roth[color=blue]
>
>[/color]


Aahz
Guest
 
Posts: n/a
#12: Jul 18 '05

re: Difference between default arguments and keyword arguments


In article <406FFEAF.3080400@netscape.net>,
DoubleM <DoublemPI@netscape.net> wrote:[color=blue]
>
>All arguments are keyword arguments. They may or may not have a default
>value.[/color]

Not quite true:

def foo(a, b):
pass

args = 1, 2
foo(*args)

def bar(*args):
pass

bar(1, 2)
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/

"usenet imitates usenet" --Darkhawk
Aahz
Guest
 
Posts: n/a
#13: Jul 18 '05

re: Difference between default arguments and keyword arguments


In article <mailman.330.1081101671.20120.python-list@python.org>,
Paul Prescod <paul@prescod.net> wrote:[color=blue]
>
>Functions implemented in C do not always have the property that every
>argument is a keyword argument. Functions implemented in Python do have
>that property. It's an implementation quirk.[/color]

Really?

def foo(*args):
pass

foo(x=1)

Traceback (most recent call last):
File "x.py", line 4, in ?
foo(x=1)
TypeError: foo() got an unexpected keyword argument 'x'
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/

"usenet imitates usenet" --Darkhawk
Aahz
Guest
 
Posts: n/a
#14: Jul 18 '05

re: Difference between default arguments and keyword arguments


In article <yD1cc.12064$yN6.3263@newsread2.news.atl.earthlink .net>,
Edward Diener <eldiener@earthlink.net> wrote:[color=blue]
>
>Along the same lines, I would like to add to that the explanation for
>classes in the tutorial is very poor since it assumes a much higher
>understanding of Python than a tutorial should about the language. A
>much simpler and more direct explanation regarding classes in Python
>would be much better. While I mostly got it because I have programmed
>extensively in other OOP languages, I would expect your average Python
>beginner to be completely lost by much of the high-level explanation in
>that chapter. Whoever wrote it was much more interested in explaining
>the theory of classes in Python, to beginners no less !, than they were
>toward explaining what classes are in Python and how to use them. And
>why iterators and generators are included in that chapter are beyond
>me. Also the tutorial mentions some usages of classes, in previous
>sections, before the explanation of classes occur, which I feel is
>definitely a mistake.[/color]

While I agree that the tutorial could stand improvement, it *is*
targetted more at experienced programmers.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/

"usenet imitates usenet" --Darkhawk
Josiah Carlson
Guest
 
Posts: n/a
#15: Jul 18 '05

re: Difference between default arguments and keyword arguments


>>Functions implemented in C do not always have the property that every[color=blue][color=green]
>>argument is a keyword argument. Functions implemented in Python do have
>>that property. It's an implementation quirk.[/color]
>
> Really?
>
> def foo(*args):
> pass
>
> foo(x=1)
>
> Traceback (most recent call last):
> File "x.py", line 4, in ?
> foo(x=1)
> TypeError: foo() got an unexpected keyword argument 'x'[/color]


Really. It just so happens that foo doesn't take any keyword arguments,
nor does it have an argument named 'x'.

- Josiah
Closed Thread