Monday, January 01, 2007

Beginning python : function

Functions are another basic part of any programming language, without it you have to rewrite/copy all your code. And knowing how bad programmers are at copying it's not a good idea. And again python beats php for this part. functions are called definitions in python.

Building functions



function name($arg1,$arg2='optional',$arg3='optional'){
echo $arg1.' '.$arg2.' '.$arg3;
}

A simple function with 2 optional arguments that shows the values of the arguments. Now the same function in python.

def name(arg1,arg2='optional',arg3='optional'):
    print arg1, arg2, arg3


The differences in syntax are:

  • def instead of function

  • open function with a : character

  • indentation of codeblocks is necessary

  • the , character instead of the . character

  • close function with a white line


You also notice that you don't have to use a string with a space to separate the values of the arguments because the print function processes the white space in the code.

Using functions



name(1);
name(1,2,3);
name(1,'optional',3);

Above you can see a few function calls for the php function. The last call shows a pain in php. You have to know the default value of an argument to access an argument later in the list. To break this behaviour you can put your optional arguments in an associative array and sort them out in the function.

function name($arg1,$array){
echo $arg1;
$arg2 = ' optional';
$arg3 = ' optional';
if(count($array) == 0){
echo $arg2.$arg3;
}else{
foreach($array as $key => $value){
if($value != ''){
switch($key){
case 'arg2': echo $arg2; break;
case 'arg3': echo $arg3; break;
}
}
}
}
}

As you can see this extends the function code quite a lot and in this example you can only use it if the optional arguments don't depend on each other. Now lets look at some python magic.

name(1)
name(1,2,3)
name(1,arg3=3)

It are the same function calls and as you can see the last one sets the argument using a keyword/value pair. You can call all arguments like that but once you start setting arguments by keyword/value pairs you can't use the value only setting any more.

name(1,arg2=2,3)

This example will cause an error. Wrong keywords also raise an error.

The setting function arguments by using key/value pairs is a real programming relief.

First of all you don't have the remember the default values of optional arguments when you want to set an argument later in the list, you don't even have to add it to your function call.

Second reason : you don't have to remember the place of arguments in the list. Remembering the argument names is easy if you use consistent argument names, remembering where you put the arguments in the list is harder if you made the function six months ago and haven't used it a lot.

Third reason : you can use an associate array (dictionary in python speech) or even an array (list in pythonese) to set the arguments.
A dictionary is build by a key/value pair(s) surrounded by curly brackets.

listtest = [1,2]
name(*listtest)
dicttest = {'arg1': 1, 'arg2': 2}
name(**dicttest)

You notice the use of the single and double asterisk to identify the datatype to unpack it in the function.
This is great because you don't have put different function calls in if statements if the arguments for the function call are different. You can build different argument dictionaries or lists and call the function in the end. This makes the code more readable.

No comments: