>>> def print_keyword_args(**kwargs):... # kwargs is a dict of the keyword args passed to the function... for key, value in kwargs.iteritems():... print "%s = %s" % (key, value)...>>> print_keyword_args(first_name="John", last_name="Doe")first_name = Johnlast_name = Doe
>>>f(1, 2)>>> args (1,2) kwargs {} #args return parameter without reference as a tuple>>>f(a = 1, b = 2)>>> args () kwargs {'a': 1, 'b': 2} #args is empty tuple and kwargs return parameter with reference as a dictionary
class Robot():# name is an arg and color is a kwargdef __init__(self,name, color='red'):self.name = nameself.color = color
red_robot = Robot('Bob')blue_robot = Robot('Bob', color='blue')
print("I am a {color} robot named {name}.".format(color=red_robot.color, name=red_robot.name))print("I am a {color} robot named {name}.".format(color=blue_robot.color, name=blue_robot.name))
>>> I am a red robot named Bob.>>> I am a blue robot named Bob.