What are the advantages of Python over Java ?, Compared

Source: Internet
Author: User

What are the advantages of Python over Java ?, Compared

Recently in the background, many people talk privately about the prospects of Python and Java? Today, let's briefly talk about the differences between the two:


I think C, Java and Python are both very successful languages. Which of the following depends on your scenario.

To Compare Python with Java.

Python:
Advantages:The syntax is concise, elegant, and powerful. The standard library and third-party library are very powerful and have a wide range of application fields: vinta/awesome-python-GitHub(A clear comparison with PHP !)

Language:

Everything is an object !!!
Class (class itself)/function/class method is callable object
Because it is an object, you can upload it. For example:

Class A: def _ init _ (self): print "init A" def run (self): print "run A" threading. thread (target = ). start () #: execute A () a = A () threading in another thread. thread (target =. run ). start () #: execute. run ()... # stunned. How did this happen ?? Maybe you should look at the descriptor concept.



List expression(There are many other languages, but I like python best ):
Change "1023" to [1, 2, 3]

[ int(x) for x in "1023" if x != "0" ]

(Currently, you can use the Lisp style map (int, filter (lambda x: x! = "0", "1024 ")))

Similar to dict:

adict = {"a":1, "b":2}{k: v*2 for k, v in adict.items() }


Yield keywords:
When you need to return many things and use them one by one, you can return one

Def xrange (n): I = 0 while I <n: yield I + = 1 usage: for I in xrange (10): print I


Decorator @:
Cache the results returned by something_heavy () (with function name + args as the cached key value, only the latest 10 keys are cached)

@ Functool. lru_cache (10) def something_heavy (args): # various time-consuming operations ...... pass


Operator overload:
Try numpy and you will be stunned. The operator vector/matrix is as convenient as the basic type.

Disadvantages:

Slow
Visual python is the slowest scripting language, which is much slower than JS on V8. (After studying the descriptor, you will find that python has a high overhead to call object methods !)

But the founder of python said that the general program bottleneck lies in 20% of the Code, so you can rewrite the 20% of the Code in C (python is the glue language, it is easy to rewrite part of the code with C)

At the same time, you can try PyPy and introduce JIT technology.

Dynamic type
Some people say that writing dynamic types is refreshing, and restructuring is a disaster (Forgive me for remembering the original words ..).

For those who do not write unittest or code coverage test, dynamic language is of course dangerous, and spelling errors may be hidden and reconstructed? Don't make trouble!

The founder of python has something to say: "If you think the compiler can help you solve all the problems, you must do programming for a long time." (I am deeply aware that I despise javascript very much when I was young and frivolous ), "testcase is required for any program. To write testcase, dynamic type is not a problem! "

We recommend two tools: py. test python-coverage.

Java:


Advantages

Use several words to describe the Java language: elegant, rigorous, robust, and error-prone.

Of course, it is also widely used: ranking first in the number of programmers all the year round!
Various libraries are also very gray: akullpp/awesome-java-GitHub
For the same code, Java code has a lower probability of bugs than C ++ (or even python) (of course, it is generally longer than python ).

Java tries its best to avoid mistakes made by programmers in terms of language design, such as automatic garbage collection, discarding unsigned types, secure enumeration, and generic check during compilation (the generic check added after Java1.5, but it is only used for Compiler checks. The runtime is the same as before.) Forced Exception Handling (catch or declarative throws must be used in case of exceptions). (Of course, python also has some features .)

Java standard library and a lot of Third-Party code are filled with a strong sense of design !!!, Various design patterns are everywhere. The following statements show that:

new BufferedInputStream(new FileInputStream(new File("somepath")))

Nima, a lot of words, Look at me:
C:

fopen("somepath", "r")

Python:

open("somepath")

There are many Java code, but the API can reuse more lines and choose more (I want to read the file at a time, instead of buffer ?)

Because of its robust security, Java has become the standard for enterprise-level backend. If you think about the banking backend, there are bugs in key areas.

Disadvantages:


A lot of code is required. refer to the example above...


Without the amazing features of Python and lack of flexibility, how can you write it in Java, while Python can write something like Javascript/Lisp/Matlab .....



In simple terms, Python is designed for humans (for example, Python). C/C ++ is more like a machine,
Java is between the two.




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
  • This article has been included in the following columns:

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.