Understanding kwargs in Python -


what uses **kwargs in python?

i know can objects.filter on table , pass in **kwargs argument.  

can specifying time deltas i.e. timedelta(hours = time1)?

how work? classes 'unpacking'? a,b=1,2?

you can use **kwargs let functions take arbitrary number of keyword arguments ("kwargs" means "keyword arguments"):

>>> def print_keyword_args(**kwargs): ...     # kwargs dict of keyword args passed function ...     key, value in kwargs.iteritems(): ...         print "%s = %s" % (key, value) ...  >>> print_keyword_args(first_name="john", last_name="doe") first_name = john last_name = doe 

you can use **kwargs syntax when calling functions constructing dictionary of keyword arguments , passing function:

>>> kwargs = {'first_name': 'bobby', 'last_name': 'smith'} >>> print_keyword_args(**kwargs) first_name = bobby last_name = smith 

the python tutorial contains explanation of how works, along nice examples.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -