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 4445
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: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |