Saturday, May 3, 2014

13. Python Functions

Functions are probably the most important element in any language. They allow for code reuse and also for separating different parts of the program.




In Python, functions are defined by the def statement. Very few functions are one line-functions. If they are, you may write the return value after the colon. This function just adds x and y (the two input arguments) and returns the value.




Most functions are multi-line. It is important to indent, after the colon. In the interactive shell, this is done automatically (indicated by the dot, dot, dot, colon prompt). You have to press 2 Enters to get out of these blocks. Here, three numbers are multiplied.




Python assumes that variables are local to a function. Here, we see that num in line 5, is a global value. In line 6, the function uses num as a local variable. The reason this is done is so we do not have to think about names while writing a function. That is, we do not have to use unique names in our functions.




If we do want to modify a global variable, we have to use the global keyword. Here the global num is modified.




Local names have precedence. Thus, in the return statement the num refers to the local num with value of 5. Thus adding 2, will give the result of 7.




However, if we do not have the local name defined in the function, it will look to any enclosing function, that is, in case it is, a nested function. Since we do not use nested functions here, the global value with that name is used. This makes sense because if you use a name without initializing it, it means you probably did want to use the global value.




In line 21, the division is imported from the __future__ module. Note, we have 2 underscores in the front and 2 underscores at the end. This is done so division of integers can produce fractional numbers. The default in Python 2.7 is integer division which is implied if both numerator and denominator are integers. Integer division results in an integer answer, that is, the number without taking any remainder into account. Next we define a function Ops, which returns 4 values. We can see that they return a tuple. In line 24, we use the underscore, which refers to the last result, in the shell, which here would mean Output of line 23.




Usually in the program, you rarely deal explicitly with the returned Tuple, but define equal number of values for left-hand side so unpacking can take place.




Default arguments can be found in a function definition. That is, if some arguments are not given in the call, then use the defaults. For the argument x in this example, we may use it's name in the call if you want. If so, we don't have to remember the argument positions.




Often you will use a Tuple as the argument. If so, you have to put a * before the name. Then, in the function you have available the various indices. You do not have to use all the indices.




If a function call names all of the arguments, we can convert it to a dictionary and then send it to the function, that is, if the function definition has 2 * symbols. Note that all names have been converted to strings in the dictionary.




If a list is input to a function, that name in the function is an alias for the called list. That is, no new lists are created. In Python, mutable values are passed by references, and so are just aliases to the same object.




If we really want a new list, we have to make a list copy. Python wants us to make this explicit since a list could be many elements and a copy could be computationally-expensive. For immutables, a copy is implied. But, we can not do this function example with tuples, as individual elements can not be reassigned.




An easy function with a list is to extend the list with the number of parameters passed. Here in the function definition, we tell it to expect a tuple. Then we extend, similar to multiple appends, to the List.




If we want, we can keep on extending the List. Remember, a List can have any kind of Python objects, like Strings, Tuples or even Lists.




You will find additional information at pythonaudio.blogspot.com.



This is the video of Tutorial 13:



2 comments:

About Me

I have used Python for the last 10+ years. I have a PhD in Electrical Engineering. I have taught Assembly Language programming of Intel-compatible chips as well as PC hardware interfacing. In my research, I have used Python to automate my calculations in physics and chemistry. I also use C++ and Java, often with Python.