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