I’ve been messing around with functions in Python. One neat feature that I came across are optional arguments. Here’s some code that I’ve written to explain more about optional arguments:
|
|
As with any other language out there, you can set default values for certain arguments in case they aren’t mentioned in a function call. For the above function, the value for bar will be 5 and 10 for foobar if the function was called like this:
|
|
Two other ways of calling function foo are:
|
|
|
|
The above function calls will override the default values for the specified argument.
Now what if you want to override the value for foobar but leave bar unchanged when calling the function? In most, if not all, language that I know, you’ll just end up doing some workaround to achieve the effect that you want. On the other hand, Python makes it possible out of the box:
|
|
I’m pretty sure this will come in handy in the future. This is also one of the coolest features I found so far.