Forwarding keyword arguments from one function to another

R

Ravi

The following code didn't work:

class X(object):
def f(self, **kwds):
print kwds
try:
print kwds['i'] * 2
except KeyError:
print "unknown keyword argument"
self.g("string", **kwds)

def g(self, s, kwds):
print s
print kwds

if __name__ == "__main__":
x = X()
x.f(k = 2, j = 10)


However the following did:

class X(object):
def f(self, **kwds):
print kwds
try:
print kwds['i'] * 2
except KeyError:
print "unknown keyword argument"
self.g("string", **kwds)

def g(self, s, **kwds):
print s
print kwds

if __name__ == "__main__":
x = X()
x.f(k = 2, j = 10)



Please explain
 
T

Tim Wintle

The following code didn't work:
def g(self, s, kwds):
print s
print kwds

This expects the function g to be called with the parameters "s" and
"kwds"

def g(self, s, **kwds):
print s
print kwds

This expects to be passed the parameter "s", and various keyword
arguments, which will be put into the dict "kwds".

when you call

o.g("string",**kwds)

you are passing the parameter "string" as the first parameter, and then
a sequence of keyword arguments taken from kwds, which will be passed
separately.

This is what the second form expects, but not what the first one
expects.

Tim Wintle
 
R

Ravi

I am sorry about the typo mistake, well the code snippets are as:

# Non Working:

class X(object):
def f(self, **kwds):
print kwds
try:
print kwds['i'] * 2
except KeyError:
print "unknown keyword argument"
self.g("string", kwds)

def g(self, s, **kwds):
print s
print kwds

if __name__ == "__main__":
x = X()
x.f(k = 2, j = 10)


# Working One

class X(object):
def f(self, **kwds):
print kwds
try:
print kwds['i'] * 2
except KeyError:
print "unknown keyword argument"
self.g("string", **kwds)

def g(self, s, **kwds):
print s
print kwds

if __name__ == "__main__":
x = X()
x.f(k = 2, j = 10)
 
A

Albert Hopkins

The following code didn't work:

class X(object):
def f(self, **kwds):
print kwds
try:
print kwds['i'] * 2
except KeyError:
print "unknown keyword argument"
self.g("string", **kwds)
^^^^^^

This means call g() with kwds passed as keyword arguments.
def g(self, s, kwds):

The method signature is not expecting keyword arguments.
print s
print kwds

if __name__ == "__main__":
x = X()
x.f(k = 2, j = 10)


However the following did:

class X(object):
def f(self, **kwds):
print kwds
try:
print kwds['i'] * 2
except KeyError:
print "unknown keyword argument"
self.g("string", **kwds)

def g(self, s, **kwds):
^^^^^^
The method signature expects (optionally) keyword arguments.
 
A

Albert Hopkins

I am sorry about the typo mistake, well the code snippets are as:

# Non Working:

class X(object):
def f(self, **kwds):
print kwds
try:
print kwds['i'] * 2
except KeyError:
print "unknown keyword argument"
self.g("string", kwds)

def g(self, s, **kwds):
print s
print kwds

if __name__ == "__main__":
x = X()
x.f(k = 2, j = 10)


# Working One

class X(object):
def f(self, **kwds):
print kwds
try:
print kwds['i'] * 2
except KeyError:
print "unknown keyword argument"
self.g("string", **kwds)

def g(self, s, **kwds):
print s
print kwds

if __name__ == "__main__":
x = X()
x.f(k = 2, j = 10)

Same reasoning, though admittedly the error text is misleading.

This example can be simplified as:

def foo(x, **kwargs):
pass
==> TypeError: foo() takes exactly 1 argument (2 given)

Again misleading, but the argument is the same (no pun intended).

However the following would have worked:

or
 
T

Terry Reedy

Ravi said:
The following code didn't work:

In the future, explain "didn't work".
Wrong output? give actual (copy and paste) and expected.
Error message? give traceback (copy and paste).
 
R

Ravi

Thnak you all.
In the future, explain "didn't work".
Wrong output? give actual (copy and paste) and expected.
Error message? give traceback (copy and paste).

I will be careful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top