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. 5 4433
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
"JonathanB" <do******@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.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='world', lingo='python')
{'hello': 'world', 'lingo': 'python'}
>>c(13,14,thenext=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)
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(**dictionary) or both, where the zuple has to come first.
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
On Jun 5, 7:31 am, "Diez B. Roggisch" <d...@nospam.web.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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)
...
|
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. ...
|
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)...
|
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...
|
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
|
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.
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |