473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Passing parameters using **kargs

I want to access parameters that are passed into a function using the
**kargs idiom. I define f(**kargs) via

def f(**kargs):
print kargs
Jul 18 '05 #1
8 6847
Thomas Philips wrote:
I want to access parameters that are passed into a function using the
**kargs idiom. I define f(**kargs) via

def f(**kargs):
print kargs
.
.

the keyword arguments are converted to a dictionary, so that if I type
f(a=1, b=2, c=3)

the function prints
{'a': 1, 'b': 2, 'c':3}

Now assume the function has three variables a, b and c to which I want
to assign the dictionary's values of 'a', 'b' and 'c'. How can I
assign kargs['a'] to a, kargs['b'] to b, and kargs['c'] to c. Should I
be trying to construct a string representation of each variable's name
and using that as a key, or am I just thinking about this the wrong
way?


How about
def f(a=None, b=None, c=None, **moreargs): .... print locals()
.... f(x=22, b=99) {'moreargs': {'x': 22}, 'a': None, 'c': None, 'b': 99}


Peter
Jul 18 '05 #2
tk****@hotmail.com (Thomas Philips) wrote in
news:b4**************************@posting.google.c om:
I want to access parameters that are passed into a function using the
**kargs idiom. I define f(**kargs) via

def f(**kargs):
print kargs
.
.

the keyword arguments are converted to a dictionary, so that if I type
f(a=1, b=2, c=3)

the function prints
{'a': 1, 'b': 2, 'c':3}

Now assume the function has three variables a, b and c to which I want
to assign the dictionary's values of 'a', 'b' and 'c'. How can I
assign kargs['a'] to a, kargs['b'] to b, and kargs['c'] to c. Should I
be trying to construct a string representation of each variable's name
and using that as a key, or am I just thinking about this the wrong
way?


If the function is to have three variables a, b, and c, then you do this:

def f(a=None, b=None, c=None, **kargs):
... whatever ...

(substitute whatever defaults you want for those variables)

The ** argument is for keyword arguments where you don't know in advance
all the keywords that might be valid. Obviously, if you don't know the name
in advance then there is no point to setting a variable of the same name
since you would have to go through pointless contortions to access it.
If you do know some names of interest in advance then make them arguments
with default values and only use the ** form for the remaining arguments.
Jul 18 '05 #3

"Duncan Booth"
Thomas Philips) >
I want to access parameters that are passed into a function using the
**kargs idiom. I define f(**kargs) via

def f(**kargs):
print kargs
The ** argument is for keyword arguments where you don't know in advance
all the keywords that might be valid.


Or if you don't care, or want to intercept invalid calls. Interestingly,
the OP's example is the beginning of a possible debug wrapper usage where
one either does not have a function's code or does not want to modify it
directly. Possible example:

_f_orig = f
def f(*largs, **kargs):
print 'f called with' largs, 'and', kargs
f(*largs, **kargs)

Terry J. Reedy


Jul 18 '05 #4
tk****@hotmail.com (Thomas Philips) wrote in message news:<b4**************************@posting.google. com>...
I want to access parameters that are passed into a function using the
**kargs idiom. I define f(**kargs) via

def f(**kargs):
print kargs
.
.

the keyword arguments are converted to a dictionary, so that if I type
f(a=1, b=2, c=3)

the function prints
{'a': 1, 'b': 2, 'c':3}

Now assume the function has three variables a, b and c to which I want
to assign the dictionary's values of 'a', 'b' and 'c'. How can I
assign kargs['a'] to a, kargs['b'] to b, and kargs['c'] to c. Should I
be trying to construct a string representation of each variable's name
and using that as a key, or am I just thinking about this the wrong
way?

Thomas Philips


MY reading of what you say is - I have three variable a, b and c.. and
a dictionary, kargs, with keys 'a', 'b' anc 'c'. I want to assign the
contents of kargs to the three variables. The specific case is surely
? :
a = kargs['a']
b = kargs['b']
c = kargs['c']

or is it the more general case you're after ?
I think I'm misunderstanding you I'm afraid....

Fuzzyman

http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #5
Since kargs is a regular dictionary variable,
you can reference the variables you want with
kargs.get('<variablename>', None)

example

kargs.get('a', None) will return value of
keyword argument a or None if 'a' doesn't
exist.

If you are sure of what will exist you can just
use kargs['a'], but then you could more easily
do f(a=None, **kargs) in that case.

kargs.keys() will give you the names of the
keyword arguments if you want them.

No real reason to put them anywhere else that I
can see.

HTH,
Larry Bates

"Thomas Philips" <tk****@hotmail.com> wrote in message
news:b4**************************@posting.google.c om...
I want to access parameters that are passed into a function using the
**kargs idiom. I define f(**kargs) via

def f(**kargs):
print kargs
.
.

the keyword arguments are converted to a dictionary, so that if I type
f(a=1, b=2, c=3)

the function prints
{'a': 1, 'b': 2, 'c':3}

Now assume the function has three variables a, b and c to which I want
to assign the dictionary's values of 'a', 'b' and 'c'. How can I
assign kargs['a'] to a, kargs['b'] to b, and kargs['c'] to c. Should I
be trying to construct a string representation of each variable's name
and using that as a key, or am I just thinking about this the wrong
way?

Thomas Philips

Jul 18 '05 #6
In article <b4**************************@posting.google.com >,
tk****@hotmail.com (Thomas Philips) wrote:
I want to access parameters that are passed into a function using the
**kargs idiom. I define f(**kargs) via

def f(**kargs):
print kargs
.
.

the keyword arguments are converted to a dictionary, so that if I type
f(a=1, b=2, c=3)

the function prints
{'a': 1, 'b': 2, 'c':3}

Now assume the function has three variables a, b and c to which I want
to assign the dictionary's values of 'a', 'b' and 'c'. How can I
assign kargs['a'] to a, kargs['b'] to b, and kargs['c'] to c. Should I
be trying to construct a string representation of each variable's name
and using that as a key, or am I just thinking about this the wrong
way?


I don't understand. Isn't this it:

Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
kargs={'a':1, 'b':2, 'c':3}
def r(a, b, c): .... print a, b, c
.... r(**kargs)

1 2 3
Use of keyword arguments doesn't NEED to be part of the
function definition.

Regards. Mel.
Jul 18 '05 #7
In article <ma*************************************@python.or g>,
Terry Reedy <tj*****@udel.edu> wrote:

Or if you don't care, or want to intercept invalid calls. Interestingly,
the OP's example is the beginning of a possible debug wrapper usage where
one either does not have a function's code or does not want to modify it
directly. Possible example:

_f_orig = f
def f(*largs, **kargs):
print 'f called with' largs, 'and', kargs
f(*largs, **kargs)


You mean

_f_orig(*largs, **kargs)

I prefer this version:

def debugParams(func):
def debugger(*args, **kwargs):
for arg in args:
print type(arg), arg
for name in kwargs:
value = kwargs[name]
print name, type(value), value
return func(*args, **kwargs)
return debugger

f = debugParams(f)
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha
Jul 18 '05 #8

"Aahz" <aa**@pythoncraft.com> wrote in message
news:ca**********@panix1.panix.com...
_f_orig = f
def f(*largs, **kargs):
print 'f called with' largs, 'and', kargs
f(*largs, **kargs)
You mean

_f_orig(*largs, **kargs)


Of course. Silly 'rushing out the door' mistake.
I prefer this version:

def debugParams(func):
def debugger(*args, **kwargs):
for arg in args:
print type(arg), arg
for name in kwargs:
value = kwargs[name]
print name, type(value), value
return func(*args, **kwargs)
return debugger

f = debugParams(f)


So do I, for its elaboration, factorization, and closure. I am saving it.

Terry J. Reedy


Jul 18 '05 #9

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

Similar topics

2
by: Edward C. Jones | last post by:
Here is a stripped-down version of a Python Cookbook recipe. Is there a simpler, more Pythonical, natural way of doing this? ------ #! /usr/bin/env python # Modified from Python Cookbook...
2
by: zlatko | last post by:
There is a form in an Access Project (.adp, Access front end with SQL Server) for entering data into a table for temporary storing. Then, by clicking a botton, several action stored procedures...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
12
by: Joel | last post by:
Hi all, Forgive me if I've expressed the subject line ill. What I'm trying to do is to call a c++ function given the following: a. A function name. This would be used to fetch a list of...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
4
by: Nathan Sokalski | last post by:
I am a beginner with AJAX, and have managed to learn how to use it when passing single parameters, but I want to return more than one value to the client-side JavaScript function that displays it....
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
0
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,...
0
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$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
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...

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.