Why/How is it that some classes require every possible argument, and some don't?
There is something called, default parameters,
def default_parameters(var1 = "cabbage"): print var1default_parameters(var1 = "salad") # saladdefault_parameters("salad") # saladdefault_parameters() # cabbagedefault_parameters("salad", "cabbage") # Error! Expects only one parameter
Here, if no parameters are passed, the default value specified in the definition is assumed.
If you want to accept many parameters, you can unpack them in the arguments section like this
def many_parameters(var1, var2 = "cabbage", *args): print var1, var2, argsmany_parameters("salad", "cabbage", "rabbit", "ninja", "puppy")# salad cabbage ('rabbit', 'ninja', 'puppy')many_parameters("salad", "cabbage", "rabbit", "ninja")# salad cabbage ('rabbit', 'ninja')many_parameters("salad", "cabbage", "rabbit")# salad cabbage ('rabbit',)many_parameters("salad", "cabbage")# salad cabbagemany_parameters("salad")# salad cabbagemany_parameters()# many_parameters() takes at least 1 argument (0 given)many_parameters("salad", var2 = "ninja", "puppy")# SyntaxError: non-keyword arg after keyword argmany_parameters("salad", "puppy", var2 = "ninja")# many_parameters() got multiple values for keyword argument 'var2'
Here, in the first case, the
salad
andcabbage
are assigned tovar1
andvar2
, but rest of the values are assigned toargs
as a tuple.The second and third cases are same as the first one.
The fourth case has got no extra parameters, so
args
will be empty.The fifth case has got no extra parameters, and no value for
var2
, soargs
will be empty and the default value (cabbage
) is assumed forvar2
.In the sixth case,
var1
hasn't got any default value provided. So, it fails withat-least 1 argument expected
error.In the seventh case,
var2
(a named variable) is used before an unnamed parameter. That is wrong in Python.In the eighth case, both
var2
(a named variable) andpuppy
(positional parameter) are being set tovar2
. Now, Python cannot decide which value to use forvar2
. So, it fails.In the previous cases, we saw how to handle default parameters and named parameters. Now, named parameters are also called as keyword arguments, which is conventionally represented as
**kwargs
, which is actually a dictionary of key-value pairs of all the named parameters. Note: Default parameters don't count as keyword parameters.def all_parameters(var1, var2 = "cabbage", *args, **kwargs): print var1, var2, args, kwargsall_parameters("salad", "cabbage", "rabbit")# salad cabbage ('rabbit',) {}all_parameters("salad", "cabbage")# salad cabbage () {}all_parameters("salad")# salad cabbage () {}all_parameters("salad", var2 = "ninja", "puppy")# SyntaxError: non-keyword arg after keyword argall_parameters("salad", "puppy", var2 = "ninja")# all_parameters() got multiple values for keyword argument 'var2'all_parameters("salad", moderator="jonclements", python="martijinpieters")# salad cabbage () {'moderator': 'jonclements', 'python': 'martijinpieters'}
I believe, except the last item all others are already covered in the previous two examples. In the last case, all the non-default but named parameters are stored in
kwargs
dictionary.
Note: Remember the *
in *args
and **
in **kwargs
, in the function definition. They are responsible for the packing of the parameters passed to the functions.
Let's take an example.
def spam(a=1, b=2, c=3): print a, b, c
This function has three arguments, all of which have default values. So I could use this function in a lot of ways.
>>> spam() # uses the default values1 2 3 >>> spam(3) # use default for b and c3 2 3>>> spam(c=4, b=2, a=6) # explicitly assign values6 2 4>>> spam(b=7) # only assign b, use defaults for a and c1 7 3
Something similar to this happens with the Canvas widget. You can see the reference here:https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/canvas.html
When declaring a method you can specify default values. Eg.:
def __init__(self, a, b, c=True, d="default value"): ....