Forwarding keyword arguments from one function to another
Albert Hopkins
marduk at letterboxes.org
Sun Feb 22 15:09:38 EST 2009
On Sun, 2009-02-22 at 11:44 -0800, Ravi wrote:
> 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.
> print s
> print kwds
>
> if __name__ == "__main__":
> x = X()
> x.f(k = 2, j = 10)
>
>
More information about the Python-list
mailing list