32.html ( File view )
- By 2010-08-21
- View(s):12
- Download(s):0
- Point(s): 1
Safari | Python Developer's Handbook -> Functions and Procedures
See All Titles |
![]() ![]() Functions and ProceduresFunctions and procedures are blocks of code that you can access from several different parts of your code. As you already know, Python gives you some built-in functions, but you can also create your own functions. Yours are called user-defined functions. Functions and procedures provide better modularity for your application and a high degree of code reusing. Procedures are functions that don't return a value. The only difference between a function and a procedure is that a procedure has either a return command without arguments (that returns None), or it doesn't have any return statement. From now on, I will use only the word function. While functions are being executed, they create their own namespace. Every time you invoke a function, such as function (a,b,c)
As you can see, tuples are an unavoidable concept inside the language. Python, by nature, allows introspection to an unprecedented degree. You can separate a function name from its parameters, store them in some place, play around with them, and later use the apply built-in function to execute the function. FunctionsFunctions always start with the abbreviation def. Their end is defined by the last line of the indented block of code that goes underneath. The general format of a function is as follows: def functionname(arg1, arg2, ): # tuple of arguments "documentation string" # optional <statements> >>> def addnumbers(x,y): "This function returns arg1 + arg2" return x + y >>> addnumbers(3,4) 9 Remember that to call a function without arguments, it's necessary to use empty parentheses. >>> variable = name() # instead of variable = name As a matter of fact, remember that you can assign functions to variables. >>> x = abs >>> print x(-2) # it's the same as saying print abs(-2) -2 x = abs returns the own function, and assigns its value to x. Python uses dynamic namespaces. In order to show that, the next example uses the value of n, available at the time of calling the function, because n isn't defined inside the function nor is it part of its list of arguments. n is part of the global namespace of the function. >>> def add_to_n(arg): return n + arg Variables that have values assigned to them inside a function always belong to the function namespace. Study the next example to learn how to change a global variable inside a function by using the keyword global. >>> x = 10 >>> def nudge(): global x x = 20 return x Python implements procedural abstraction. Although this topic has a scary name, it is something very easy and simple. Python offers this feature by providing anonymous functions implemented with the keyword lambda. This type of abstraction can be used when the function is just an expression. In other words, lambda is just another way of writing def, except that it doesn't have to be named, and you can only put an expression in it. (The return is implicit.) It is intended to be just a shorthand to write small functions easier as shown in the following: >>> f = lambda x: x * 2 >>> f(20) 40 The previous case can also be written as follows: >>> def f(x): return x * 2 >>> f(30) 60 Here's another example: >>> def compose(func1,func2,y): f = lambda x, f1=func1, f2=func2: f1(f2(x)) return f(y) >>> compose(chr,abs,-65) 'A' Note that in this last example, it is necessary to pass the default arguments to the lambda function because Python has only local and global namespaces. lambda is very useful for functionssuch as map, filter, and reducethat need a function as an argument. >>> def listtostring(list): return reduce(lambda string, item: string + chr(item), list, "") >>> listtostring([1,2,3,4,5]) "\001\002\003\004\005" ParametersAll parameters (arguments) in the Python language are passed by reference. Modules, classes, instances, and other functions can be used as arguments to functions and examined dynamically. Keep in mind that you don't need to specify the object type of an argument. By default, arguments have a positional behavior, and you need to inform them in the same order that they were defined. >>> def powerdivision(x,y): return x/y >>> pri ... ... (Not finished, please download and read the complete file) ...
Expand> <Close
Sponsored links
File listTips: You can preview the content of files by clicking file names^_^
|
Your Point isn't enough.
Get point immediately by PayPal
More(Debit card / Credit card / PayPal Credit / Online Banking)