I've run across code like "myfunction(x, *someargs, **someotherargs)",
but haven't seen documentation for this.
Can someone fill me in on what the leading * and ** do? Thanks.
Stephen 7 2905
Stephen Boulet wrote: I've run across code like "myfunction(x, *someargs, **someotherargs)", but haven't seen documentation for this.
Can someone fill me in on what the leading * and ** do? Thanks.
Stephen
See item 7.5 in Language Reference. In short: * declaries list of
additional parameters, while ** declares map of additional parameters.
Try somehting like:
def foo(*params): print params
and
def bar(**params): print params
to find out details.
regards,
anton.
anton muhin <an********@rambler.ru> wrote in news:bv3gdg$ms0sn$1@ID-
217427.news.uni-berlin.de: Stephen Boulet wrote: I've run across code like "myfunction(x, *someargs, **someotherargs)", but haven't seen documentation for this.
Can someone fill me in on what the leading * and ** do? Thanks.
Stephen
See item 7.5 in Language Reference. In short: * declaries list of additional parameters, while ** declares map of additional parameters. Try somehting like:
def foo(*params): print params
and
def bar(**params): print params
to find out details.
I may be wrong, but I think the OP was asking about calling functions
rather than defining them. The '*' and '**' before arguments in function
calls are described in section 5.3.4 of the language reference but, so far
as I know, isn't explained clearly in any of the more 'user level' text.
There is a brief mention in the library reference under the documention of
'apply'.
In a call of the form:
myfunction(x, *someargs, **someotherargs)
the sequence 'someargs' is used to supply a variable number of additional
positional arguments. 'someotherargs' should be a dictionary and is used to
supply additional keyword arguments.
anton muhin wrote: Stephen Boulet wrote:
I've run across code like "myfunction(x, *someargs, **someotherargs)", but haven't seen documentation for this.
Can someone fill me in on what the leading * and ** do? Thanks.
Stephen
See item 7.5 in Language Reference. In short: * declaries list of additional parameters, while ** declares map of additional parameters. Try somehting like:
def foo(*params): print params
and
def bar(**params): print params
to find out details.
regards, anton.
<<
a={'c1':'red','c2':'blue','c3':'fusia'}
def bar(**params):
.....for k in params.keys():
.........print k, params[k]
bar(a)
Traceback (most recent call last):
File "<input>", line 1, in ?
TypeError: bar() takes exactly 0 arguments (1 given)
Why does bar take zero arguments?
Hmm, but:
<<
def bar2(*params,**moreparams):
.....print "params are ", params,'\n'
.....for k in moreparams.keys():
.........print k, moreparams[k]
.....print "moreparams are ", moreparams
bar2(range(3),a,)
params are ([0, 1, 2], {'c3': 'fusia', 'c2': 'blue', 'c1': 'red'})
moreparams are {}
I think I've got the *params argument down (you just get the whole
argument list), but I'm still not getting the **moreparams ...
Stephen
Stephen Boulet wrote: << a={'c1':'red','c2':'blue','c3':'fusia'} def bar(**params): ....for k in params.keys(): ........print k, params[k] bar(a) Traceback (most recent call last): File "<input>", line 1, in ? TypeError: bar() takes exactly 0 arguments (1 given) >> Why does bar take zero arguments?
The error message is not as clear as it should should be. bar() takes 0
mandatory, 0 positional and an arbitrary number of keyword arguments, e.
g.: def bar(**params):
.... print params
.... bar(color="blue", rgb=(0,0,1), name="sky")
{'color': 'blue', 'rgb': (0, 0, 1), 'name': 'sky'}
That's the standard way of passing optional keyword arguments, but you can
also pass them as a dictionary preceded by **:
adict = {'color': 'blue', 'rgb': (0, 0, 1), 'name': 'sky'} bar(**adict)
{'color': 'blue', 'rgb': (0, 0, 1), 'name': 'sky'}
This is useful, if you want to compose the argument dict at runtime, or pass
it through to a nested function. Hmm, but:
<< def bar2(*params,**moreparams): ....print "params are ", params,'\n' ....for k in moreparams.keys(): ........print k, moreparams[k] ....print "moreparams are ", moreparams
bar2() accepts 0 mandatory, as many positional and keyword arguments as you
like. For the expected result, try
def bar2(*args, **kwd):
.... print args
.... print kwd
.... bar2(1,2,3)
(1, 2, 3)
{} bar2(1, name="sky", color="blue")
(1,) # tuple of optional positional args
{'color': 'blue', 'name': 'sky'} # dict of optional keyword args
And now a bogus example that shows how to compose the arguments:
def bar3(x, *args, **kwd):
.... print "x=", x
.... print "args=", args
.... print "kwd=", kwd
.... if "terminate" not in kwd:
.... kwd["terminate"] = None # avoid infinite recursion
.... bar3(x*x, *args + ("alpha",), **kwd)
.... bar3(2, "beta", color="blue")
x= 2
args= ('beta',)
kwd= {'color': 'blue'}
x= 4
args= ('beta', 'alpha')
kwd= {'color': 'blue', 'terminate': None}
To further complicate the matter, a mandatory arg may also be passed as a
keyword argument:
bar3(x=2, color="blue") # will work, args is empty
bar("beta", # will not work; both "beta" and 2 would be bound to x
# and thus create a conflict
x=2, color="blue")
Peter
On Mon, 26 Jan 2004 09:51:02 -0600, Stephen Boulet
<stephendotboulet@motorola_._com> wrote: I've run across code like "myfunction(x, *someargs, **someotherargs)", but haven't seen documentation for this.
Can someone fill me in on what the leading * and ** do? Thanks.
Try section 4.7.2 of the tutorial.
--
Eric Amick
Columbia, MD
On Mon, 26 Jan 2004 20:15:22 -0500, Eric Amick wrote: Try section 4.7.2 of the tutorial.
Matter of fact, try the whole tutorial, beginning to end. It will
answer many questions you haven't thought of yet.
<http://www.python.org/doc/tut/>
--
\ "Those are my principles. If you don't like them I have |
`\ others." -- Groucho Marx |
_o__) |
Ben Finney <http://bignose.squidly.org/>
In article <bv************@ID-217427.news.uni-berlin.de>,
anton muhin <an********@rambler.ru> wrote: See item 7.5 in Language Reference. In short: * declaries list of additional parameters, while ** declares map of additional parameters.
Actually, ``*`` forces creation of a tuple: def foo(*bar):
.... print type(bar)
.... l = [1,2,3] foo(*l)
<type 'tuple'>
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/
"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code --
not in reams of trivial code that bores the reader to death." --GvR This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: sinister |
last post by:
The examples in the online manual all seem to use double quotes, e.g. at
http://us3.php.net/preg_replace
Why? (The behavior is different with single quotes, and presumably simpler
to...
|
by: Hattuari |
last post by:
On page 32 of TC++PL SE is an example of a complex class as follows:
class complex {
double re, im;
public:
complex(double r, double i){re=r; im=i;}
//...
};
On page 262 of TC++PL SE is an...
|
by: Joel |
last post by:
Hi,
I incorporated a function in my code that whenever I use a string variable
in an sql statement if the string contains a single quote it will encase it
in double quotes else single quotes.
...
|
by: Bhan |
last post by:
Hi,
Can I get answers for the following:
1. Usage of Double Pointers during allocation memory.Heard that if
memory is to be allocated by someone who is working on another
module,then we...
|
by: Jason S |
last post by:
is there any way to use templates to bind integer/floating point
constants to a template for compile-time use?
e.g.
template <double conversion>
class meters
{
const factor = conversion;
| |
by: Grant Edwards |
last post by:
I'm pretty sure the answer is "no", but before I give up on the
idea, I thought I'd ask...
Is there any way to do single-precision floating point
calculations in Python?
I know the various...
|
by: desktop |
last post by:
This page:
http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html
start with the line: "Virtual functions allow polymorphism on a single
argument". What does that exactly mean?
I guess it...
|
by: amygdala |
last post by:
Hi all,
I'm starting this new project in which I'ld like to implement sort of a
design pattern I have seen being used in the CodeIgniter framework.
Basically, the site will examine the URI and...
|
by: Bill Reid |
last post by:
I just noticed that my "improved" version of sscanf() doesn't assign
floating point numbers properly if the variable assigned to is declared
as a "float" rather than a "double". (This never...
|
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...
|
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,...
| |
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |