473,666 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

*args and **kwargs

Ok, this is probably definitely a newbie question, but I have looked
all over the Python library reference material and tutorials which I
can find online and I cannot find a clear definition of what these are
and more importantly how to use them. From what I can tell from their
use in the examples I've seen, they are for passing a variable number
of arguments to a function (which I need to do in a program I am
working on). But how do you use them? Is there a fixed order in which
the arguments within *arg or **kwarg should be passed or will be
called within a function? I realize this probably involves a long-
winded answer to a very simple and common programming problem, so if
someone has a link to TFM, I'll gladly go RTFM. I just can't find it.

Jun 5 '07 #1
5 4456
JonathanB wrote:
Ok, this is probably definitely a newbie question, but I have looked
all over the Python library reference material and tutorials which I
can find online and I cannot find a clear definition of what these are
and more importantly how to use them. From what I can tell from their
use in the examples I've seen, they are for passing a variable number
of arguments to a function (which I need to do in a program I am
working on). But how do you use them? Is there a fixed order in which
the arguments within *arg or **kwarg should be passed or will be
called within a function? I realize this probably involves a long-
winded answer to a very simple and common programming problem, so if
someone has a link to TFM, I'll gladly go RTFM. I just can't find it.
That's because it's in the language reference, not in the library reference.

http://docs.python.org/ref/calls.html

Diez
Jun 5 '07 #2
"JonathanB" <do******@gmail .comwrote in message
news:11******** **************@ q75g2000hsh.goo glegroups.com.. .
Ok, this is probably definitely a newbie question, but I have looked
all over the Python library reference material and tutorials which I
can find online and I cannot find a clear definition of what these are
and more importantly how to use them. From what I can tell from their
use in the examples I've seen, they are for passing a variable number
of arguments to a function (which I need to do in a program I am
working on). But how do you use them? Is there a fixed order in which
the arguments within *arg or **kwarg should be passed or will be
called within a function? I realize this probably involves a long-
winded answer to a very simple and common programming problem, so if
someone has a link to TFM, I'll gladly go RTFM. I just can't find it.

I hope this example code will help you understand:
>>def a(*stuff):
print repr(stuff)
>>def b(**stuff):
print repr(stuff)

>>def c(*args, **kwargs):
print 'args', repr(args)
print 'kwargs', repr(kwargs)

>>a(1,2,3)
(1, 2, 3)
>>b(hello='worl d', lingo='python')
{'hello': 'world', 'lingo': 'python'}
>>c(13,14,thene xt=16,afterthat =17)
args (13, 14)
kwargs {'afterthat': 17, 'thenext': 16}
>>args = [1,2,3,4]
kwargs = {'no-way': 23, 'yet-anotherInvalid. name': 24}
c(*args, **kwargs)
args (1, 2, 3, 4)
kwargs {'no-way': 23, 'yet-anotherInvalid. name': 24}
>>>

(sorry for the messed-up formatting)
Jun 5 '07 #3
JonathanB wrote:
Ok, this is probably definitely a newbie question, but I have looked
all over the Python library reference material and tutorials which I
can find online and I cannot find a clear definition of what these are
and more importantly how to use them. From what I can tell from their
use in the examples I've seen, they are for passing a variable number
of arguments to a function (which I need to do in a program I am
working on). But how do you use them? Is there a fixed order in which
the arguments within *arg or **kwarg should be passed or will be
called within a function? I realize this probably involves a long-
winded answer to a very simple and common programming problem, so if
someone has a link to TFM, I'll gladly go RTFM. I just can't find it.

http://www.python.org/doc/faq/progra...ion-to-another
(the first hit when you search python.org for *args and **kwargs)

Basically 'args' is a tuple with all the positional arguments, kwargs is
a dictionary with all the named arguments.
Likewise you can pass a tuple to a function like func(*tuple), or a dict
like func(**dictiona ry) or both, where the zuple has to come first.
Jun 5 '07 #4
I hope this example code will help you understand:
>>Code Snipped<<
OOH!! That makes perfect sense, thanks!, *args are passed as a turple,
**kwargs are passed as a dictionary. That means **kwargs is probably
what I want.

JonathanB
Jun 5 '07 #5
On Jun 5, 7:31 am, "Diez B. Roggisch" <d...@nospam.we b.dewrote:
JonathanB wrote:
Ok, this is probably definitely a newbie question, but I have looked
all over the Python library reference material and tutorials which I
can find online and I cannot find a clear definition of what these are
and more importantly how to use them.
Also, well maybe op did not check THE tutorial. Or found explanation
too terse. But it's there.

http://docs.python.org/tut/node6.htm...00000000000000

rd


Jun 5 '07 #6

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

Similar topics

3
2377
by: Alexander Eberts | last post by:
I'm new to python so appologies to the group if this question is asked often. I'm wondering if it's possible to query an object instance to find out what arguments the instance's __init__ function was called with from *outside* the class's local namespace. For example, if I define a class Foo as follows: import sys class Foo: def __init__(self, *args): print args # no problem here
3
2374
by: python | last post by:
When I pass a function as an arg, like for map(...), how do I pass args to use for that function? If I have a function like this: def pretty_format(f, fmt='%0.3f'): return fmt % f I want to use it with map() like this:
1
2001
by: Achim Domma | last post by:
Hi, I'm using py2exe to create a exe out of a python script. If works fine so far, but a get lot's of this warnings: warning: use func(*args, **kwargs) instead of apply(func, args, kwargs) I tried the last stable version of py2exe and the prerelease for 0.5. Both show this behavior. Can somebody tell me what the message means and how to avoid it?
2
9633
by: Jim Jewett | last post by:
Normally, I expect a subclass to act in a manner consistent with its Base classes. In particular, I don't expect to *lose* any functionality, unless that was the whole point of the subclass. (e.g., a security-restricted version, or an interface implementation that doesn't require a filesystem.) One (common?) exception seems to occur in initialization. I understand stripping out arguments that your subclass explicitly handles or...
15
2978
by: Stefan Behnel | last post by:
Hi! I'm trying to do this in Py2.4b1: ------------------------------- import logging values = {'test':'bla'} logging.log(logging.FATAL, 'Test is %(test)s', values) -------------------------------
26
9605
by: lbolognini | last post by:
Hi all, I have a very long list of parameters coming from a web form to my method foo(self, **kwargs) I would like to avoid manually binding the variables to the values coming through the **kwargs dictionary, just to keep the code cleaner, I'd like to bind them automatically I was adviced against doing it by adding stuff to locals such as:
3
1930
by: Mark E. Fenner | last post by:
Hello all, I was migrating some code from sets.ImmutableSet to frozenset and noticed the following: ******code******** #!/usr/bin/env python from sets import ImmutableSet
13
1361
by: dmitrey | last post by:
I need something like this: def func1(*args, **kwargs): if some_cond: return func2(*args, **kwargs) else: return func3(some_other_args, **kwargs) Thank you in advance, D.
3
4244
by: Sean DiZazzo | last post by:
Why is the following not working? Is there any way to get keyword arguments working with exposed XMLRPC functions? ~~~~~~~~~~~~~~~~ server.py import SocketServer from SimpleXMLRPCServer import SimpleXMLRPCServer,SimpleXMLRPCRequestHandler # Threaded mix-in class
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8355
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8550
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8638
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7381
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5662
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
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 we have to send another system

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.