Ever wondered what the
**kwargs that you keep seeing in Python functions mean? I certainly did. So I did some searching and it turns they're pretty nifty, not to mention useful.
* and the
** operators both perform two different, but complementary operations depending on where they're used. When used in a method definition, like so:
def __init__(self, *args, **kwargs): pass
They perform an operation called 'packing'. True to it's name, what this does is pack all the arguments that this method call receives into one single variable, a tuple called
args. You can use any variable name you want, of course, but
args seems to be the most common and Pythonic way of doing things.
Once you have this 'packed' variable, you can do things with it that you would with a normal tuple.
args would give you the first and second argument, respectively. If you convert the args tuple to a list you can also modify, delete and re-arrange items in it.
So how do you pass these packed arguments to another method? Here's where unpacking comes in to play:
def __init__(self, *args, **kwargs): # do some stuff super(AwesomeClass, self).__init__(self, *args, **kwargs) # ^ # LOOK HERE!
So there's the same
* operator again, but this time it's in the context of a method call. What it does now is explode the
args array and call the method as if you'd typed in each variable separately.
Here's another example that might make things a little more clear:
def func1(x, y, z): print x print y print z def func2(*args): # Convert args tuple to a list so we can modify it args = list(args) args = 'Hello' args = 'awesome' func1(*args) func2('Goodbye', 'cruel', 'world!') # Will print # > Hello # > awesome # > world!
This happens simply because we're changing the first two arguments before passing them off to
The normal rules governing method definition apply here… calling
func2('a', 'b', 'c', 'd') will raise an error because it will in turn call
func1 with four arguments, which it doesn't expect.
The same principle applies to
**kwargs too, except that in this case it applies to keyword arguments, and
kwargs turns out to be a
Combined together packing and unpacking lets you do a lot of things like:
I'm sure there's a lot more uses you can think of and see for these little
*s. Have fun.
Edits & Credits:
super(self.__class__, self).__init__()would result in infinite recursion if a subclass is written and used.
selfwould still refer to the instance of the subclass when the call comes up the chain.
argsbeing a tuple.