It used to be very rare to use Python, now more and more, found a lot of python surprises me, one of which is script short. Why is it? Can it be said that Python has the characteristics of functional programming?
Reply content:
- is designed for different purposes. C + +/Java itself is the language of serious engineering, and Python/perl is called the "scripting language" because they are designed to be executed as scripts, and often are also responsible for "gluing" other programs to work together to accomplish tasks. (Although Python is the most engineering-biased in a variety of scripting languages)
- pushed down from this design, these languages must be more convenient to perform some common functions, otherwise people can use java/c++ directly. So Bash is exceptionally good at invoking and assembling programs, and Perl is especially good at string processing. Python is relatively common, providing a relatively fast track approach and easy-to-write syntax for the purpose:
- Dynamic strongly typed omits the type declaration. C + + 11 is just beginning to introduce auto for type deduction. The latter, of course, is a bit stronger for the right guarantee. The
- contains rich and efficient data types: array/list/dict. Compare the new HashMap > (). Get ("Key1"). Get ("Key2") or something ...
- Python introduced some of the elements of the FP language earlier, such as Map/filter/apply/itertools, and the big artifact of Lisp comprehension and generator [1]. Compared to C + + 11/java 8 to introduce lambda is ... Tear eyes.
- Standard library, "battery included". Although in some respects than the JRE, than the C + + std lib that weak X can also be proud to laugh.
- and Python is stronger with PyPI and matching PIP tools, making management dependencies, automatic installation and updates as easy as the Debian package installation (it's not black, really). Java was later also a Maven (given the need for handwritten XML, I still think the goods are not for human use.) Clojure Leiningen slightly better). C + + Corner cry!
Of course, if the Lord really intends to go astray and look for the LANGUAGE, it's okay to watch the Ruby and Lisp languages ...
Forget Paul Graham or who said that ... Each language will eventually become a Lisp dialect:)
[1] http://www. dabeaz.com/generators/g enerators.pdf
I think the main is:
- Built in string, Unicode, Bytes, tuple, list, dict, set, etc., plus operator overloading;
- For ... in statement;
- list comprehension;
- Other infrastructure, such as ZIP (), map (), reduce (), inclusive standard libraries, and so on.
You might want to compare some of the strongly typed static languages, such as C + +, Java, and so on. It's not a lot shorter than Ruby and Haskell. So let's compare it to C + +.
Personally, Python has been less "fancy" since C++11 introduced new features, including anonymous functions, range-based loops (for ... in), Auto, and enhanced STL. So, the most convenient feature of Python is that it can be "short":
- List comprehension. This should be the most convenient, right? Sometimes C + + in order to initialize an array of 2^n, you need to write a for loop specifically. In Python, a list comprehension can be done directly. However, if abused and nested multiple, it makes readability very poor.
- Convenient built-in types, such as list native support slice, and dict/set are handy for c++11 std::string, but Std::unordered_map has basically caught up with Dict.
- Named argument and tuple as function return types make it much easier to define a flexible function.
- Dynamic types of languages can be more flexible, but also more error-prone. Beginners are often planted on type matches. As a C + + born programmer, I know what I am doing, is basically easier to compile at a time, but also very easy to find grammatical errors where (here to thank clang).
- Rich Library of functions. PyPI can directly install a variety of package, and there are numpy/scipy + matplotlib and other artifacts, the MATLAB matrix computing convenience has also attracted over, but also derived from a variety of domain package, such as NLP, machine le arning, network analysis, data mining .... C + + seems to be a lot less (at least not a unified, easy-to-search repo).
- It is worth mentioning that indentation represents a block of code. There's Pros & cons here, and it's certainly a lot more lines than {}, but it seems more prone to error.
1. Dynamic type
2. Powerful built-in data type: list,dict
3.first Order function
4.list Comprehension & Corountine (although this is controversial, but the official said there is it when he has it)
5.library more than Java:
(1) Python supports functional programming without writing a Hello world to write several lines of code.
(2) Python does not have a strongly typed constraint, and a variable can be used to accept any type assignment with a prior declaration.
(3) Python does not have so many specifications in Java, such as the need to write Getter,setter. Python exception captures can not declare the catch exception type.
(4) Python supports operator overloading, and Java can only be written in the form of methods such as those in BigDecimal.
(5) Python does not use {} to constrain a block of statements.
(6) The 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 so designed, and the JavaBean specification seems to say that the setter return type should be void.
(7) The Python parameter pass can pass the function directly (everything is object), and Java can only pass the object (method is not an object), the large number of callback in Android directly cause the code ugly!!!
(8) The python function parameter supports default values, while Java can only be implemented by polymorphism;
(9) Python supports lambda
Wait a minute....
The only thing that bothers Python is decode. Isn't it automatic GC and built-in Lib?