please read the update to this post : Using *args, reloaded!
So we’ve been actively working on our latest project in django – and I came across this weird syntax in views
def view_function(request, *args, **kwargs)
Digging in further, I found that *args stands for argument list, and **kwargs stands for keyword argument list ( well, only the asterik and double asterik matters, the names can be anything ). So, why is this useful again?
def foo(*args): for i, arg in enumerate(*args): print "Argument ", i, " : ", arg
and call it by
foo("a", "b", "c")
Simply put, we now have a way to call a function with arbitrary number of parameter. The function foo can be passed with any number of arguments.
So, that’s about *args. Now what is **kwargs? It is the keyword argument list – which means you can pass the keywords as well as their values as a dictionary.
class Foo(object): def __init__(self, value): print value class DerivedFoo(Foo): def __init__(self, *args, **kwargs): print 'DerivedFoo' super(DerivedFoo, self).__init__(*args, **kwargs) myFoo = DerivedFoo("Calling Foo through DerivedFoo")
The super keyword can be used to call the methods of superclass – in this case the init of class Foo. The parameters which are passed to inherited class can be passed to the base class as shown above. This can be used to extend the behavior of the base class, without knowing anything about base class.