Python is rarely used. Now it is used more and more. I have found a lot of things that surprise me. One of them is that the script is short and concise. Why? Can it be said that python has the functionalprogramming feature? Python is rarely used. Now it is used more and more. I have found a lot of things that surprise me. One of them is that the script is short and concise. Why? Can it be said that python has the functional programming feature? Reply content:
- Different design objectives. C ++/Java is a language designed for serious engineering purposes, and Python/Perl is called a "scripting language" because they are designed as scripts and temporarily executed, it is also generally responsible for "gluing" other programs to collaborate to complete the task. (Although Python is the most engineering-oriented in various scripting languages)
- From this design purpose, these languages must be easier to execute some common functions. Otherwise, you can simply use Java/C ++. Therefore, Bash is particularly good at calling and composing programs, and Perl is especially good at string processing. Python is relatively common and provides a relatively short and convenient syntax for writing:
- The dynamic strong type statement is omitted. C ++ 11 just introduced auto for Type derivation. Of course, the latter provides a better guarantee for correctness.
- Built-in rich and efficient data types: array/list/dict. Compare new HashMap > (). Get ("Key1"). get ("Key2 ......
- Python introduced some FP language elements earlier, such as map/filter/apply/itertools, as well as lisp comprehension and generator [1. Compared with C ++ 11/Java 8, lambda is introduced ...... Tears.
- Standard library, "battery supported ded ". Although it is worse than JRE in some aspects, X, which is weaker than C ++ std lib, can still be proud to laugh.
- In addition, Python is better equipped with PyPI and supporting pip tools, making it as easy to manage dependencies, automatic installation, and update as it is to install the Debian system package (this is not black, true ). Java later came along with Maven (in view of the need to hand-write XML, I still think this product is not intended for humans. The Leiningen of Clojure is slightly better ). C ++ is crying!
Of course, if THE subject really wants to go astray looking for the language, it's okay to look around THE Ruby and Lisp languages ......
I forgot whether it was Paul Graham or who said it ...... Each language will eventually become a lisp dialect :)
[1] http://www.dabeaz.com/generators/Generators.pdf
I think the main reasons are:
- Built in string, unicode, bytes, tuple, list, dict, set, etc., plus operator overload;
- For... in statement;
- List comprehension;
- Other infrastructure, such as zip (), map (), reduce (), and other standard libraries.
You may want to compare some strong static languages, such as C ++ and Java. Compared with ruby and haskell, it may not be much shorter. So let's compare it with C ++.
I personally feel that since C ++ 11 introduced those new features, including anonymous functions, range-based loop (... in), auto, and enhanced STL, python seems less "fancy. So, what makes python the most convenient is that it can be "short:
- List comprehension. Should this be the most convenient? Sometimes C ++ needs to write a for loop to initialize a 2 ^ n Array. In python, you can directly perform a list comprehension. However, misuse of multiple nesting Causes Poor readability.
- Convenient built-in types, such as list native support for slice, and dict/set are very convenient to use std: string in C ++ 11, but std :: unordered_map has basically caught up with dict.
- Named argument and tuple are used as function return types, making it much easier to define a flexible function.
- Dynamic languages can be more flexible but error-prone. Many beginners use type matching. As a C ++ programmer, I know what I am doing is basically easier to compile at a time, and it is also easy to find out where the syntax is wrong (thanks to clang ).
- Rich function libraries. Pypi can directly install various packages, and has the existence of numpy/scipy + matplotlib and other artifacts, which also attracts the convenience of matlab matrix calculation. Therefore, various domain packages are derived, such as NLP, machine learning, network analysis, data mining .... C ++ seems to be much inferior (at least there is no unified and convenient repo for search)
- It is worth mentioning that indentation represents a code block. Its pros & cons is indeed much less than {}, but it also seems more error-prone.
1. Dynamic type
2. Powerful built-in data types: list, dict
3. first order function
4. list comprehension & corountine)
5. The number of libraries is too large for java:
(1) python supports functional programming and does not need to write several lines of code without writing hello world.
(2) python does not have a strong type constraint. variables are declared in advance. A variable can be assigned a value of any type.
(3) python does not have so many java specifications, such as getter and setter. Python exception capture does not declare the capture exception type.
(4) python supports Operator overloading. java can only write methods, such as those in BigDecimal.
(5) In python, {} is not used to restrict statement blocks.
(6) methods in python are generally designed to be chained (most of the official libraries have this design), while the official libraries in java are basically not designed like this, in addition, it seems that the setter return type should be void in the javabean specification.
(7) python parameter passing can directly pass functions (all objects), while java can only pass objects (Method is not an object). A large number of callback in Android can directly lead to ugly code !!!
(8) python function parameters support default values, while java can only be implemented through polymorphism;
(9) python supports lambda
And so on ....
The only annoying thing about python is decode. Isn't it automatic gc and built-in lib?