Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Difference between default arguments and keyword arguments

Reply
Thread Tools

Difference between default arguments and keyword arguments

 
 
Edward Diener
Guest
Posts: n/a
 
      04-04-2004
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 ?


 
Reply With Quote
 
 
 
 
DoubleM
Guest
Posts: n/a
 
      04-04-2004
Edward Diener wrote:
> 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 ?
>
>

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:
>>> def fn(a,b):

print 'a = ',a
print 'b = ',b


>>> fn(b=1,a=2)

a = 2
b = 1
>>> fn(1,2)

a = 1
b = 2
>>>

Hope this helps,
Mike
 
Reply With Quote
 
 
 
 
Edward Diener
Guest
Posts: n/a
 
      04-04-2004
DoubleM wrote:
> Edward Diener wrote:
>> 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 ?
>>
>>

> 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:
> >>> def fn(a,b):

> print 'a = ',a
> print 'b = ',b
>
>
> >>> fn(b=1,a=2)

> a = 2
> b = 1
> >>> fn(1,2)

> a = 1
> b = 2


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


 
Reply With Quote
 
Marco Herrn
Guest
Posts: n/a
 
      04-04-2004
On 2004-04-04, DoubleM <(E-Mail Removed)> wrote:
> 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.


But what does this mean?:

>>> __import__(name="eggs")

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 http://www.velocityreviews.com/forums/(E-Mail Removed)
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

 
Reply With Quote
 
Paul Prescod
Guest
Posts: n/a
 
      04-04-2004
Marco Herrn wrote:

> On 2004-04-04, DoubleM <(E-Mail Removed)> wrote:
>
>>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.

>
>
> But what does this mean?:
>
> >>> __import__(name="eggs")

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: __import__() takes no keyword arguments


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

 
Reply With Quote
 
Paul Prescod
Guest
Posts: n/a
 
      04-04-2004
Marco Herrn wrote:

> On 2004-04-04, DoubleM <(E-Mail Removed)> wrote:
>
>>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.

>
>
> But what does this mean?:
>
> >>> __import__(name="eggs")

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: __import__() takes no keyword arguments


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



 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      04-04-2004
Edward Diener wrote:
> ... Makes sense. Thanks ! The tutorial should explain it more clearly.


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
(E-Mail Removed)
 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      04-04-2004

"Edward Diener" <(E-Mail Removed)> wrote in message
news:UmTbc.9904$(E-Mail Removed) ink.net...
> 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

?

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


 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      04-04-2004
"John Roth" <(E-Mail Removed)> wrote:
> 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.


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.
 
Reply With Quote
 
Edward Diener
Guest
Posts: n/a
 
      04-05-2004
Roy Smith wrote:
> "John Roth" <(E-Mail Removed)> wrote:
>> 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.

>
> 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.


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.


 
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
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
Keyword arguments in a block, possible with zero arguments? Peter Motzfeldt Ruby 1 03-13-2007 01:33 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments