Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize independent Jython program

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to implement a stand-alone Jython program. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The following listing is an example of a stand-alone Jython program:

Listing 7. Sample Jython program (listing7.py) for simulating coin toss

From java.util import Random rng = Random () # This is a comment in Jython print "Flipping a coin..." If rng.nextBoolean (): print "Came up heads" else: print "Came up tails"

Before explaining how to run the code, let's explain the code. This example introduces the if statement in Jython, which is one of the first aspects of some people's comments about Jython (and its ancestors, Python). No character delimiter marks the block of code to execute when the if statement condition is true (conditions in Jython do not require enclosed parentheses, as in Java programming). It's just that the code is indented one layer more than the surrounding code.

Jython code blocks are always marked with indentation instead of other tags, such as curly braces. Statements that introduce code blocks, such as if, end with a colon. This feature of Jython means that you have to be careful when writing code, because the way you indent the code may actually change the meaning of the code. For example, listing 8a produces a printout with only the number 3, because both of its statements belong to if blocks whose conditions are never true:

Listing 8a. Indentation of Jython code: only print "3"

If 0: print "1" print "2" print "3"

If I only change the indentation of one line, the numbers 2 and 3 will be printed:

Listing 8b. Indentation of Jython code: print "2" and "3"

If 0: print "1" print "2" print "3"

Indentation must also be consistent, it must be associated with statements that organize the code into blocks, and usually it must also control the code flow. For example:

Listing 8c. Indentation of Jython code: syntax error

Print "1" print "2" print "3"

This only produces a syntax error because there is no control statement that requires a block to be separated from the rest of the code.

Using indentation to mark code blocks is one of the more controversial features of Python and Jython, but I think this problem is often exaggerated. After all, if you follow a good coding standard for indentation, you shouldn't have this problem. If good coding indentation is followed, the machine will execute and peer critics will have nothing to say, so facts speak louder than words.

In addition, I know that when developers use the language for a while, no one will pay attention to this limitation. Proper indentation becomes the second nature of Jython. This connection between indentation and syntax can of course cause errors that have never been encountered before, but the lack of explicit delimiters also eliminates some common errors in languages that use them.

You can run the file in listing 7 (listing7.py) without compiling, simply calling the file name as an argument to the jython command, as shown below:

Listing 9. Run "coin toss" without compiling

$jython listing7.py Flipping a coin... Came up tails $

In the previous example, $is the UNIX shell prompt, which is very similar to C:\ > on Windows systems. You can also use the jpythonc command to compile the module into a Java bytecode (.class) file, which allows you to run it directly using the java or jre command. Jython modules compiled in this way have some limitations, but this issue is beyond the scope of this article.

Thank you for reading! This is the end of this article on "how to achieve independent Jython programs". 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 out 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: 287

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report