473,508 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between default arguments and keyword arguments

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 ?
Jul 18 '05 #1
14 5199
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
Jul 18 '05 #2
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.
Jul 18 '05 #3
On 2004-04-04, DoubleM <Do*******@netscape.net> 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 he***@gmx.net
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

Jul 18 '05 #4
Marco Herrn wrote:
On 2004-04-04, DoubleM <Do*******@netscape.net> 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

Jul 18 '05 #5
Marco Herrn wrote:
On 2004-04-04, DoubleM <Do*******@netscape.net> 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

Jul 18 '05 #6
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
Sc***********@Acm.Org
Jul 18 '05 #7

"Edward Diener" <el******@earthlink.net> wrote in message
news:Um*****************@newsread3.news.atl.earthl 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
Jul 18 '05 #8
"John Roth" <ne********@jhrothjr.com> 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.
Jul 18 '05 #9
Roy Smith wrote:
"John Roth" <ne********@jhrothjr.com> 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.
Jul 18 '05 #10

"Edward Diener" <el******@earthlink.net> wrote in message
news:yD******************@newsread2.news.atl.earth link.net...
Roy Smith wrote:
"John Roth" <ne********@jhrothjr.com> 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 certainly wouldn't object to removing them; that's why I said "if".
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.
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

Jul 18 '05 #11
In article <40**************@netscape.net>,
DoubleM <Do*******@netscape.net> wrote:

All arguments are keyword arguments. They may or may not have a default
value.


Not quite true:

def foo(a, b):
pass

args = 1, 2
foo(*args)

def bar(*args):
pass

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

"usenet imitates usenet" --Darkhawk
Jul 18 '05 #12
In article <ma**************************************@python.o rg>,
Paul Prescod <pa**@prescod.net> wrote:

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.


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 (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"usenet imitates usenet" --Darkhawk
Jul 18 '05 #13
In article <yD******************@newsread2.news.atl.earthlink .net>,
Edward Diener <el******@earthlink.net> wrote:

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.


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

"usenet imitates usenet" --Darkhawk
Jul 18 '05 #14
>>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.


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'

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

- Josiah
Jul 18 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2776
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
4
5829
by: aling | last post by:
What's the rule of default argument of function in C++? I found that the default argument of function could not necessary be a constant value. Is it true? Previously I thought that the default...
11
3589
by: Raj Dhrolia | last post by:
Hi Guys, It might seem to be a very easy question, but i am very much eager to know some good technical difference between C# and VB.NET. Are there anything that i can do in one language and...
50
3266
by: LaundroMat | last post by:
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to...
12
2374
by: André | last post by:
Hi, i'm learning working with classes. In class "classbase1", i defined an overridable function. In the class "subclass1", i defined the same function with 'Overrides'. In class "classbase2", i...
6
2248
by: fcvcnet | last post by:
Hi, I read the book C++ Primer, Fourth Edition By Stanley B. Lippman, Jos¨¦e Lajoie, Barbara E. Moo "If we define a class using the class keyword, then any members defined before the first...
35
2162
by: bukzor | last post by:
I've found some bizzare behavior when using mutable values (lists, dicts, etc) as the default argument of a function. I want to get the community's feedback on this. It's easiest to explain with...
7
1434
by: George Sakkis | last post by:
A situation that often comes up is having to initialize several instance attributes that accept a default value. For a single class, passing the default values in __init__ is fine: class...
0
875
by: Scott David Daniels | last post by:
Nathan Duran wrote: Several things are false (for example: '', 0, False, , ...) If you can get along with the code you have suggested, I'd think about using: def franklin(self, keyword):...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.