In python, We Can modularize some functions, just like in java, and put some function-related or identical code together, so that we can directly call it when needed.
Benefits:
1. Once a function module has been written, it can be called in the future, and code reuse can be reflected.
2. After the function is written, no error will occur. If a function is the same, we write it again in one module, and write it again in another module ...... in this way, we will inevitably ensure that no errors will occur during the writing process.
However, if we can write a function module, we will use it in many places. First, it is convenient to use, and second, it can ensure its correctness.
3. Code sharing
If you have said so much, it is better to come to reality !!!
Create a new file: (of course, this name can be used as needed)
fib(n): a,b = 0,1 b < (b,end= a,b = b, a + fib2(n): result = a,b = 0,1 b < a,b = b, a + add(numberA,numberB): numberA + sub(numberA,numberB): numberA - mul(numberA,numberB): numberA* div(numberA,numberB): numberB != numberA//
The above is a simple custom function module, which defines some methods.
We need to call the following method in the file: (another file in the same directory as the file:
fibo.fib(1000 result = fibo.fib2(1000 (result)
Running effect:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32, >>> ================================ RESTART ================================>>> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 9871, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987>>>
Now we have defined another file:
Some methods in the call
fibo fib(100 result = fib2(1000 (result)
As described above, we have applied the fib and fiber 2 methods in the file.
Running effect:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32, >>> ================================ RESTART ================================>>> 1 1 2 3 5 8 13 21 34 55 891, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987>>>
If we want to apply all the methods, we can do this:
fibo * fib(1000 (fib2(100 numberA = 20 numberB = 5 (,numberA,,numberB, (,numberA,,numberB, (,numberA,,numberB, (,numberA,,numberB,,div(numberA,numberB))
Running effect:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32, >>> ================================ RESTART ================================>>> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 9871, 1, 2, 3, 5, 8, 13, 21, 34, 55, 8920 + 5 = 2520 - 5 = 1520 * 5 = 10020 / 5 = 4>>>