Python functions

1336492798000 » Tagged as: Python , Ruby , vargs

We've already taken quick glance at python functions elsewhere, and know that they are created with the def keyword, can have default parameters and which part of the code belongs to the function is identified by it's indentation level When using default parameters, the PHP approach is that you need to specify the first few parameters and the remainder will be initialized with the defaults. However you cannot 'skip' parameters. For example you can't specify values param1, param2 and then param4 instead of param3. Even if param3 is defined to take a default value the call will fail. This behavior is something that PHP has in common with python except for one thing;  you can use name, value pairs to specify only some of the parameters and they do not have to be in order. An example from the python docs will make this clearer.  
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
    print "-- This parrot wouldn't", action,
    print "if you put", voltage, "volts through it."
    print "-- Lovely plumage, the", type
    print "-- It's", state, "!"

parrot(1000)                                          # 1 positional argument
parrot(voltage=1000)                                  # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM')             # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000)             # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump')         # 3 positional arguments
parrot('a thousand', state='pushing up the daisies')  # 1 positional, 1 keyword

I said except for one thing , but hang on a second there is one more;  default values are initialized only once, so make sure that you don't change the parameters with in the function or there will be hell to pay.

Many of us have a nasty habit of using parameters that were passed in for temporary storage inside the function. If you have this habit it's perhaps better to use variable number of arguments (in python speak this is arbitrary argument lists) instead of default values.

 def my_method(var1, var2, *var3)

Is that Ruby code or python code? Actually I copy pasted that from my recent post on ruby. The syntax is exactly the same the only difference is that Ruby says var3 will be an array and python says it's a list, but it's a classic case of a rose by any other name. As with ruby the * has nothing to do with the * in C   Alternatively, instead of a list or array you can pass in a hash (in ruby) or a dictionary in python as the set of arguments. However if you want to use this approach in python *var3 above should be changed to **var3.   The Salty Crane explains that it's possible to do the reverse; to pass in a dictionary or a list to a function that does not expect it and isn't even ready to accept a variable list of arguments. Here's how:
def test_var_args_call(arg1, arg2, arg3):
    print "arg1:", arg1
    print "arg2:", arg2
    print "arg3:", arg3t

kwargs = {"arg3": 3, "arg2": "two"}
test_var_args_call(1, **kwargs)
One last thing before you go; lets look at lambda functions. This is how you create an anonymous function in python. But they are not very readable and restricted to just one expression. In short they are almost useless but let's look at an example (from the python docs anyway)  
lambda x: x + n
What the hell is that? Python's answer to inline functions.
comments powered by Disqus