In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shares with you the content of the sample analysis of Jython grammar. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The first is the classic example program-- Hello WorldBright-- which is shown in Jython as follows:
Listing 1. Jython's Hello World!
Print "Hello World!"
In Jython syntax (as in Python), the source file uses the extension "py". When the "Hello World" statement is placed in a source file such as hello.py, it is a complete program.
For comparison, take a look at the equivalent Java program and look at the following hello.java file:
Listing 2. Java's Hello World!
Public class hello {public static void main (String [] args) {System.out.println ("Hello World!");}}
The Jython syntax is obviously simpler in format, which is usually the case. The good thing about Jython syntax is that the code can be interpreted directly, or it can be translated into Java source code and automatically compiled into class files. You can run the Hello World program with the following Java command:
C:\ > java-Dpython.home=C:\ jython-2.1-cp C:\ jython-2.1\ jython.jar org.python.util.jython hello.py
You can also run the program with the following more convenient Jython script:
C:\ > jython hello.py
Echo, echo.py!
Then, try an example with some added functionality, which is a Mini Program that allows you to echo command-line arguments. For the program echo, use the following code (which is in echo.py):
Listing 3. A sample program that echoes command line parameters
Import sys count = 1 for arg in sys.argv [1:]: print 'Argument% iTunes% s'% (count, arg) count + = 1
Again, this complete Jython program reveals some key features of the Jython syntax. The thing to note is that each line is a statement-- you don't need a semicolon (;) to end those lines-- and the variable is not declared.
You should also note that in Jython syntax, command-line arguments are accessed through the built-in symbol sys.argv, which is a series of strings. Sys is a standard module that contains useful values and functions, and there are many other standard modules available. * the argv element (0) is the program name (that is, echo.py). To skip it, take the fragment of the argv list, actually start at one (1), and then iterate until the list is *.
In addition, in Jython grammar, loops are implemented through for statement groups. The body of the for statement is the lines indented after for. The print statement demonstrates the use of the string formatting capabilities of Jython (similar to the new print features of printf of Cpica Clipper + and Java 1.5).
The above code can also be written more accurately in the following form:
From sys import argv for i in range (1, len (argv)): print 'Argument% iTunes% s'% (I, argv [I])
In this example, range is used to index the sys.argv list. Because the argv variable is imported directly, there is no need to qualify. Note that the length of the argv list is obtained through the len function rather than as a method, which is a usage in Jython syntax and is common in many languages. There are many other functions available.
Thank you for reading! This is the end of this article on "sample Analysis of Jython Grammar". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.