473,396 Members | 1,915 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,396 software developers and data experts.

what does **kw mean?

I've been reading the following example, and couldn't figure out, what
**kw mean. (It's an empty dictionary, but what's the semantics):
def wrap(method):
def wrapped(self, *args, **kw):
print "begin"
method(self, *args, **kw)
print "end"
return wrapped
class Test(object):
def method(self, name):
print "method(%r)" % name

t = Test()
t.method("pure")
Test.method = wrap(Test.method)
t.method("wrapped")

Jan 11 '08 #1
4 15846
Does this help?

def foobar(first_name,last_name, *args, **kwargs):
print first_name
print last_name
print "Tuple:",args
print "Dict:",kwargs

x = "has"
y = "demonstrated"
foobar('Shane','Geiger', x, y, adjective='useful', thing='PYTHON trick
known as extended call syntax', adverb='wonderfully')


zs****@gmail.com wrote:
I've been reading the following example, and couldn't figure out, what
**kw mean. (It's an empty dictionary, but what's the semantics):
def wrap(method):
def wrapped(self, *args, **kw):
print "begin"
method(self, *args, **kw)
print "end"
return wrapped
class Test(object):
def method(self, name):
print "method(%r)" % name

t = Test()
t.method("pure")
Test.method = wrap(Test.method)
t.method("wrapped")


--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

Jan 11 '08 #2
zs****@gmail.com a écrit :
I've been reading the following example, and couldn't figure out, what
**kw mean. (It's an empty dictionary, but what's the semantics):
Keyword varargs. And FWIW, *args is for positional varargs.

Jan 11 '08 #3
Lie
On Jan 11, 4:38*pm, "zsl...@gmail.com" <levili...@gmail.comwrote:
I've been reading the following example, and couldn't figure out, what
**kw mean. (It's an empty dictionary, but what's the semantics):
It's a keyword argument. It's some kind of repository for arguments
that aren't recognized.

If you have function like this:
def func(a, *args, *kw):
print a
print args
print kw

and you call the functin like this:
func('value A', 'value B', 'value C', argumentA = 'value D', argumentB
= 'value D')
the extra arguments would normally raise an error, but with the * and
**, Python would:
- assign 'value B' and 'value C' to args
- assign 'argumentA':'value D' and 'argumentB':'value E' to kw

so if you run the function, it will output:
####
value A
('value B', 'value C')
{'argumentB': 'value E', 'argumentA': 'value D'}
####

this args and kw can be accessed like a tuple and dictionary
respectively

See '4.7.2 Keyword Arguments' and '4.7.3 Arbitrary Argument Lists' on
Python Help File
Jan 11 '08 #4
On Jan 11, 12:24 pm, Lie <Lie.1...@gmail.comwrote:
On Jan 11, 4:38 pm, "zsl...@gmail.com" <levili...@gmail.comwrote:
I've been reading the following example, and couldn't figure out, what
**kw mean. (It's an empty dictionary, but what's the semantics):

It's a keyword argument. It's some kind of repository for arguments
that aren't recognized.

If you have function like this:
def func(a, *args, *kw):
print a
print args
print kw

and you call the functin like this:
func('value A', 'value B', 'value C', argumentA = 'value D', argumentB
= 'value D')
the extra arguments would normally raise an error, but with the * and
**, Python would:
- assign 'value B' and 'value C' to args
- assign 'argumentA':'value D' and 'argumentB':'value E' to kw

so if you run the function, it will output:
####
value A
('value B', 'value C')
{'argumentB': 'value E', 'argumentA': 'value D'}
####

this args and kw can be accessed like a tuple and dictionary
respectively

See '4.7.2 Keyword Arguments' and '4.7.3 Arbitrary Argument Lists' on
Python Help File
Thanks!
Jan 11 '08 #5

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
1
by: Åâãåíèé Êîñåíêî | last post by:
Hi! I make my first steps in Tkinter/Tix programming. The next program is work well (really, _almost_ well): import Tkinter root = Tkinter.Tk() widget = Tkinter.Button(root)
3
by: fowlertrainer | last post by:
Hello ! I see these things in many sources, under wxPy: def __init__(self,params,**kw,**args) What are these parameters, and what's the meaning of the double * ? Thanx for any info.
7
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that...
30
by: Reinhold Birkenfeld | last post by:
Hello, I know that there are different YAML engines for Python out there (Syck, PyYaml, more?). Which one do you use, and why? For those of you who don't know what YAML is: visit...
11
by: Rob Conner | last post by:
I'm still working on my DateTime class from last week... Why does __init__ not get called? The docs at http://www.python.org/dev/doc/devel/ref/customization.html read "If __new__() returns an...
10
by: rh0dium | last post by:
Hi all, Below is a basic threading program. The basic I idea is that I have a function which needs to be run using a queue of data. Early on I specified my function needed to only accept basic...
8
by: Viktor | last post by:
Can somebody give me an explanation what happened here (or point me to some docs)? Code: HMMM = None def w(fn): print 'fn:', id(fn) HMMM = fn
5
by: Miki | last post by:
Hello All, I'm try to write the C equivalent of: def kw(*args, **kw): print "%d args" % len(args), if "default" in kw: print "default is %s" % kw else: print "no default"
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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
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...
0
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,...
0
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...

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.