In-depth analysis of Python script Functions

Source: Internet
Author: User

For many developers, the Python scripting language should be heard. However, there are also various comments about the Python scripting language, what's more, the Python scripting language will disappear into the development family for a certain period of time.

I am not going to comment on others' right and wrong ideas here. I just want to talk about Python as a computer language from a more objective perspective. Python is an explanatory language and belongs to OpenSource projects. However, you can use it for commercial purposes.

It can also be sold on commercial product CDs. Of course, people can also get their installation programs or source code free of charge from www.Python.org or OpenSource. Python was originally compiled on an Apple computer, but now it can run on a mainstream operating platform in the world. It is not only for Windows.

In addition, Linux, Unix, and many other platforms can use it. In RedHat9, it is also installed as a required language for development. Python code types can be divided into three types: byte code and binary code optimization code.

Their suffixes are. py. pyc. pyo, which can be directly run. No compilation or connection is required. With these types of code, Python selects different types in different situations. As follows:

 
 
  1. [thread2.py]  
  2.  
  3. import thread  
  4.  
  5. import time  
  6.  
  7. input = None 
  8.  
  9. lock = thread.allocate_lock()  
  10.  
  11. def threadProc():  
  12.  
  13.     while True:   
  14.  
  15.         print 'sub thread id : ', thread.get_ident()  
  16.  
  17.         print 'sub thread %d wait lock...' % thread.get_ident()  
  18.  
  19.         lock.acquire()  
  20.  
  21.         print 'sub thread %d get lock...' % thread.get_ident()  
  22.  
  23.         print 'sub thread %d receive input : %s' % (thread.get_ident(), input)  
  24.  
  25.         print 'sub thread %d release lock...' % thread.get_ident()  
  26.  
  27.         lock.release()  
  28.  
  29.         time.sleep(1)  
  30.  
  31. thread.start_new_thread(threadProc, ())  
  32.  
  33. print 'main thread id : ', thread.get_ident()  
  34.  
  35. while True:  
  36.  
  37.     print 'main thread %d wait lock...' % thread.get_ident()  
  38.  
  39.     lock.acquire()  
  40.  
  41.     print 'main thread %d get lock...' % thread.get_ident()  
  42.  
  43.     input = raw_input()  
  44.  
  45.     print 'main thread %d release lock...' % thread.get_ident()  
  46.  
  47.     lock.release()  
  48.  
  49.     time.sleep(1)  

At runtime, The Python script language detects. is there MyPython in the current directory of py. python file. If not, import MyPython. py and generate MyPython. pyc prepares for the next faster import. This reduces the initialization time. In addition, the Python script language automatically completes all this without user intervention. His syntax is relatively simple among many other languages, and he has ruled out a lot of tedious thinking. As follows:

 
 
  1. #! /Usr/bin/python
  2. A=2003 
  3. Print
  4. A='Hello python' 
  5. Print
  6. A= [2, 0, 0, 3]
  7. Print a [0]
  8.  
  9. The above code can be run normally, and the result is as follows:
  10.  
  11. 2003
  12. Hello python
  13. 2

For C ++, at least three sets of variables need to be declared. For the Python script language, a variable is enough. In addition, Python also has its own uniqueness in managing code blocks. The following is a comparison between Python and C ++ for the same While loop.

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.