|
| 1 | +Comparing Python to Other Languages |
| 2 | +----------------------------------- |
| 3 | + |
| 4 | +These comparisons are a personal view. Comments are requested. |
| 5 | +--Guido van Rossum < [email protected]> |
| 6 | + |
| 7 | +Python is often compared to other interpreted languages such as Java, |
| 8 | +JavaScript, Perl, Tcl, or Smalltalk. Comparisons to C++, Common Lisp |
| 9 | +and Scheme can also be enlightening. In this section I will briefly |
| 10 | +compare Python to each of these languages. These comparisons |
| 11 | +concentrate on language issues only. In practice, the choice of a |
| 12 | +programming language is often dictated by other real-world constraints |
| 13 | +such as cost, availability, training, and prior investment, or even |
| 14 | +emotional attachment. Since these aspects are highly variable, it |
| 15 | +seems a waste of time to consider them much for this publication. |
| 16 | + |
| 17 | +Java |
| 18 | + |
| 19 | +Python programs are generally expected to run slower than Java |
| 20 | +programs, but they also take much less time to develop. Python |
| 21 | +programs are typically 3-5 times shorter than equivalent Java |
| 22 | +programs. This difference can be attributed to Python's built-in |
| 23 | +high-level data types and its dynamic typing. For example, a Python |
| 24 | +programmer wastes no time declaring the types of arguments or |
| 25 | +variables, and Python's powerful polymorphic list and dictionary |
| 26 | +types, for which rich syntactic support is built straight into the |
| 27 | +language, find a use in almost every Python program. Because of the |
| 28 | +run-time typing, Python's run time must work harder than Java's. For |
| 29 | +example, when evaluating the expression a+b, it must first inspect the |
| 30 | +objects a and b to find out their type, which is not known at compile |
| 31 | +time. It then invokes the appropriate addition operation, which may be |
| 32 | +an overloaded user-defined method. Java, on the other hand, can |
| 33 | +perform an efficient integer or floating point addition, but requires |
| 34 | +variable declarations for a and b, and does not allow overloading of |
| 35 | +the + operator for instances of user-defined classes. |
| 36 | + |
| 37 | +For these reasons, Python is much better suited as a "glue" language, |
| 38 | +while Java is better characterized as a low-level implementation |
| 39 | +language. In fact, the two together make an excellent |
| 40 | +combination. Components can be developed in Java and combined to form |
| 41 | +applications in Python; Python can also be used to prototype |
| 42 | +components until their design can be "hardened" in a Java |
| 43 | +implementation. To support this type of development, a Python |
| 44 | +implementation written in Java is under development, which allows |
| 45 | +calling Python code from Java and vice versa. In this implementation, |
| 46 | +Python source code is translated to Java bytecode (with help from a |
| 47 | +run-time library to support Python's dynamic semantics). |
| 48 | + |
| 49 | +Javascript |
| 50 | + |
| 51 | +Python's "object-based" subset is roughly equivalent to |
| 52 | +JavaScript. Like JavaScript (and unlike Java), Python supports a |
| 53 | +programming style that uses simple functions and variables without |
| 54 | +engaging in class definitions. However, for JavaScript, that's all |
| 55 | +there is. Python, on the other hand, supports writing much larger |
| 56 | +programs and better code reuse through a true object-oriented |
| 57 | +programming style, where classes and inheritance play an important |
| 58 | +role. |
| 59 | + |
| 60 | +Perl |
| 61 | + |
| 62 | +Python and Perl come from a similar background (Unix scripting, which |
| 63 | +both have long outgrown), and sport many similar features, but have a |
| 64 | +different philosophy. Perl emphasizes support for common |
| 65 | +application-oriented tasks, e.g. by having built-in regular |
| 66 | +expressions, file scanning and report generating features. Python |
| 67 | +emphasizes support for common programming methodologies such as data |
| 68 | +structure design and object-oriented programming, and encourages |
| 69 | +programmers to write readable (and thus maintainable) code by |
| 70 | +providing an elegant but not overly cryptic notation. As a |
| 71 | +consequence, Python comes close to Perl but rarely beats it in its |
| 72 | +original application domain; however Python has an applicability well |
| 73 | +beyond Perl's niche. |
| 74 | + |
| 75 | +Tcl |
| 76 | + |
| 77 | +Like Python, Tcl is usable as an application extension language, as |
| 78 | +well as a stand-alone programming language. However, Tcl, which |
| 79 | +traditionally stores all data as strings, is weak on data structures, |
| 80 | +and executes typical code much slower than Python. Tcl also lacks |
| 81 | +features needed for writing large programs, such as modular |
| 82 | +namespaces. Thus, while a "typical" large application using Tcl |
| 83 | +usually contains Tcl extensions written in C or C++ that are specific |
| 84 | +to that application, an equivalent Python application can often be |
| 85 | +written in "pure Python". Of course, pure Python development is much |
| 86 | +quicker than having to write and debug a C or C++ component. It has |
| 87 | +been said that Tcl's one redeeming quality is the Tk toolkit. Python |
| 88 | +has adopted an interface to Tk as its standard GUI component library. |
| 89 | + |
| 90 | +Smalltalk |
| 91 | + |
| 92 | +Perhaps the biggest difference between Python and Smalltalk is |
| 93 | +Python's more "mainstream" syntax, which gives it a leg up on |
| 94 | +programmer training. Like Smalltalk, Python has dynamic typing and |
| 95 | +binding, and everything in Python is an object. However, Python |
| 96 | +distinguishes built-in object types from user-defined classes, and |
| 97 | +currently doesn't allow inheritance from built-in types. Smalltalk's |
| 98 | +standard library of collection data types is more refined, while |
| 99 | +Python's library has more facilities for dealing with Internet and WWW |
| 100 | +realities such as email, HTML and FTP. Python has a different |
| 101 | +philosophy regarding the development environment and distribution of |
| 102 | +code. Where Smalltalk traditionally has a monolithic "system image" |
| 103 | +which comprises both the environment and the user's program, Python |
| 104 | +stores both standard modules and user modules in individual files |
| 105 | +which can easily be rearranged or distributed outside the system. One |
| 106 | +consequence is that there is more than one option for attaching a |
| 107 | +Graphical User Interface (GUI) to a Python program, since the GUI is |
| 108 | +not built into the system. |
| 109 | + |
| 110 | +C++ |
| 111 | + |
| 112 | +Almost everything said for Java also applies for C++, just more so: |
| 113 | +where Python code is typically 3-5 times shorter than equivalent Java |
| 114 | +code, it is often 5-10 times shorter than equivalent C++ code! |
| 115 | +Anecdotal evidence suggests that one Python programmer can finish in |
| 116 | +two months what two C++ programmers can't complete in a year. Python |
| 117 | +shines as a glue language, used to combine components written in C++. |
| 118 | + |
| 119 | +Common Lisp and Scheme |
| 120 | + |
| 121 | +These languages are close to Python in their dynamic semantics, but so |
| 122 | +different in their approach to syntax that a comparison becomes almost |
| 123 | +a religious argument: is Lisp's lack of syntax an advantage or a |
| 124 | +disadvantage? It should be noted that Python has introspective |
| 125 | +capabilities similar to those of Lisp, and Python programs can |
| 126 | +construct and execute program fragments on the fly. Usually, |
| 127 | +real-world properties are decisive: Common Lisp is big (in every |
| 128 | +sense), and the Scheme world is fragmented between many incompatible |
| 129 | +versions, where Python has a single, free, compact implementation. |
0 commit comments