[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

what does **kw mean?

levilista@gmail.com

1/11/2008 9:38:00 AM

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")

4 Answers

Shane Geiger

1/11/2008 9:59:00 AM

0

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')




zslevi@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
sgeiger@ncee.net | 402-438-8958 | http://ww...

Leading the Campaign for Economic and Financial Literacy

Bruno Desthuilliers

1/11/2008 10:52:00 AM

0

zslevi@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.

Lie Ryan

1/11/2008 11:24:00 AM

0

On Jan 11, 4:38 pm, "zsl...@gmail.com" <levili...@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):

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

levilista@gmail.com

1/11/2008 5:21:00 PM

0

On Jan 11, 12:24 pm, Lie <Lie.1...@gmail.com> wrote:
> On Jan 11, 4:38 pm, "zsl...@gmail.com" <levili...@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):
>
> 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!