Example analysis of module usage in Python development

Source: Internet
Author: User
Tags mul
The examples in this paper describe the use of module modules in Python development. Share to everyone for your reference, as follows:

In Python, we can modularize some of the functionality, just a little bit like in Java, putting together some function-related or identical code so that when we need it, we can call it directly.

The benefits of doing this:

1, as long as a functional module has been written, it can be called later, code reuse can be reflected

2, after the function has been written, no error will occur. If one function is the same, we write it again in a module and write it again in another module ... This way we are sure that we do not make mistakes in the process of writing.

But if we can write a functional module after writing, we put him in a lot of places, one, easy to use, and second, to ensure its correctness

3, code sharing

Said so much, or to some practical better!!!

We create a new file: fibo.py (of course, this name can be as personal as you wish)

#Modulesdef fib (n):    # write Fibonacci series up to n a b  = 0,1 while  b < n:    print (b,end= ') A, b    = b, a + B  print () def fib2 (n):    # return Fibonacci series up to n  result = [] A, a  = 0,1 while  b < n:
  result.append (b)    A, B = B., A + b  return resultdef Add (numbera,numberb): #定义加法  return Numbera + NUMBERBD EF Sub (numbera,numberb): #定义减法  return numbera-numberbdef mul (numbera,numberb): #定义乘法  return numbera* Numberbdef Div (numbera,numberb): #定义除法  if Numberb! = 0:    return numbera//numberb  else:    return ' Error '

The above is a simple function module that we have customized, which defines some methods.

We need to call the method in fibo.py in: test_modules.py (and the fibo.py file in another file in the same directory):

Import FIBOFIBO.FIB (+) result = FIBO.FIB2 (+) print (result)

The results are as follows:

Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]>>>

Now we have a file defined:

Test_modules1.py calling some of the methods in fibo.py

From Fibo import fib,fib2# here is the FIB (two) result = Fib2 () print (result) that can be used directly with the above methods

As described above, we have applied the FIB,FIB2 two methods in the fibo.py file

Operating effect:

Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> 1 2 3 5 8 [1, 1, 2, 3, 5, 8, A,, 144, 233, 377, 610, 987]>>>

If we are going to apply all the methods in fibo.py, we can do this:

#应用fibo. Py All method # is a bit similar to the introduction package form in Java from Fibo Import * #这里可以使用fibo中的所用方法fib (FIB2) Numbera = 20numberB = 5print (' addition calculation: ', Numbera, ' + ', numberb, ' = ', add (Numbera,numberb)) print (' Subtraction calculation: ', Numbera, '-', numberb, ' = ', sub (Numbera, Numberb) print (' Multiplication calculation: ', Numbera, ' * ', numberb, ' = ', Mul (Numbera,numberb)) print (' Division calculation: ', Numbera, '/', numberb, ' = ', div ( Numbera,numberb))

Operating effect:

Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] Addition calculation: 20 + 5 = 25 subtraction Calculation : 20-5 = 15 multiplication Calculation: 20 * 5 = 100 Division calculation: 20/5 = 4>>>

I hope this article is helpful for Python program design.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.