On the function of things, always say, the following list some of the notes to write functions. Special statement, these matters are not my summary, I was copied from a book named "Learning Python", by the way written in Chinese, of course, is translated according to their own perspective, which also mixed with some of their own views. Reader can also be understood to derive from "Learning Python" but a little different.
• function is independent. It is often said not to have too strong coupling. To allow functions to be independent of things outside. Parameters and return statements are the best way to achieve this independence.
• Try not to use global variables, which is also a way to let functions have a low degree of coupling. Global variables, although the function of internal and external communication, but it strengthens the function of the external dependencies, often make the function of the modification and debugging of the program more cumbersome.
• If the object of the parameter is a mutable type of data, in a function, do not modify it. Of course, more often than not, parameters are passed in the best immutable.
• Functions and goals of function implementation should be single. At the beginning of each function, a short sentence is needed to illustrate the function and purpose of the function.
• Function should not be too large, small is small, according to the principle of the previous one, the function of a single goal, then the number of code bar is small. If it feels a bit large, see if it can be disassembled, for several functions.
• Do not modify variables in another module file. This is the same as the previous reason, the goal is to reduce the coupling.
try a little bit of recursion.
For the use of recursion in Python, I have a cautious attitude, can not be used, why not? On the one hand fearing themselves not fine, in addition, recursion not only consumes resources, but also many times the speed is not as fast as for the loop.
However, as a programmer, it is necessary to understand the recursive return. Here is a simple example.
Copy Code code as follows:
>>> def newsum (LST):
. If not LST:
... return 0
. else:
... return lst[0] + newsum (lst[1:])
...
>>> newsum ([1,2,3])
6
This is a function that sums the list (reader may have thought, does it not have a sum built-in function in Python to sum it?). Why do you write it yourself? Yes, in the actual programming, there is no need to write on their own, with sum on it. In this example, it is purely to illustrate recursion, there is no programming practice, of course, I do not know whether the parameters passed to the function is a complete list of numbers, so if you enter the list of letters, it will be programmed like this:
Copy Code code as follows:
>>> Newsum ([1,2,3, ' Q '])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in Newsum
File "<stdin>", line 5, in Newsum
File "<stdin>", line 5, in Newsum
File "<stdin>", line 5, in Newsum
Typeerror:cannot concatenate ' str ' and ' int ' objects
This is the flaw of this function. But, to illustrate recursion, we don't care so much. Ignore this shortcoming for the moment. Reader note the above function, there is a sentence: return LST (0) +newsum (lst[1:]), in this sentence, and called the side of the function itself. Yes, that's recursive, call the function itself in the function. The difference, of course, is that the incoming parameters change. To clear the call flow of the function, we can print each parameter passed in:
Copy Code code as follows:
>>> def newsum (LST):
... print lst
. If not LST:
... return 0
. else:
... return lst[0] + newsum (lst[1:])
...
>>> newsum ([1,2,3])
[1, 2, 3]
[2, 3]
[3]
[]
6
This is recursion.
In fact, reader may have thought that, even without sum, you can use for to do the above operation.
Copy Code code as follows:
>>> LST = [1,2,3]
>>> Sum_result = 0
>>> for x in Lst:sum_result + = X
...
>>> Sum_result
6
Remember: Functions are objects
Remember, in the first part of the study, the constant emphasis: variable no type, data types, then encountered data including strings, numbers, lists, tuples, dictionaries, files, these things are considered objects. Functions are similar to them and are objects. So you can assign values, pass to other functions, embed data structures, return from one function to another, and so on, like previous objects. Of course, the object of a function is also specific, that is, it can be invoked by a list parameter in parentheses following a function expression.
Copy Code code as follows:
>>> def newsum (LST): #依然以这个递归的函数为例
... print lst
. If not LST:
... return 0
. else:
... return lst[0] + newsum (lst[1:])
...
>>> LST = [1,2,3]
>>> newsum (LST) #这是前面已经常用的方法
[1, 2, 3]
[2, 3]
[3]
[]
6
>>> Recusion_fun = newsum #通过赋值语句, let variable recusion_fun also reference function Newsum (LST) object
>>> Recusion_fun (LST) #从而变量能够实现等同函数调用的操作
[1, 2, 3]
[2, 3]
[3]
[]
6
One more example, in this case, be sure to remember that the function is an object. Reader ever remember? In a list, you can hold any object, so is it possible to hold a function?
Copy Code code as follows:
>>> fun_list = [(newsum,[1,2,3]), (newsum,[1,2,3,4,5])]
>>> for Fun,arg in fun_list:
... fun (ARG)
...
[1, 2, 3]
[2, 3]
[3]
[]
6
[1, 2, 3, 4, 5]
[2, 3, 4, 5]
[3, 4, 5]
[4, 5]
[5]
[]
15
function, really is the object ah.
Now that you're an object, you can view the information in Dir (object):
Copy Code code as follows:
>>> dir (newsum)
[' __call__ ', ' __class__ ', ' __closure__ ', ' __code__ ', ' __defaults__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __format__ ', ' __get__ ', ' __getattribute__ ', ' __globals__ ', ' __hash__ ', ' __init__ ', ' __module__ ', ' __name__ ', ' __new__ ', ' __reduce __ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' func_closure ', ' func_ Code ', ' Func_defaults ', ' func_dict ', ' func_doc ', ' func_globals ', ' func_name '
>>> dir (newsum.__code__)
[' __class__ ', ' __cmp__ ', ' __delattr__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __gt__ ', ' __ Hash__ ', ' __init__ ', ' __le__ ', ' __lt__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' co_argcount ', ' co_cellvars ', ' co_code ', ' co_consts ', ' co_filename ', ' co_ Firstlineno ', ' co_flags ', ' co_freevars ', ' co_lnotab ', ' co_name ', ' co_names ', ' co_nlocals ', ' co_stacksize ', ' co_ Varnames ']
>>> newsum.__code__.__doc__
' Code (Argcount, Nlocals, stacksize, Flags, codestring, constants, names,\n varnames, filename, name, Firstlineno, Lno tab[, freevars[, Cellvars]] \n\ncreate a code object. Not for the faint of heart. '
>>> Newsum.__code__.co_varnames
(' LST ',)
>>> Newsum.__code__.co_argcount
1
So, you reader, when you use a function, you first put it on the object's level, it's not something special, and although we use a lot of space to tell it, it's still an object.