Using PythonIn USENIX ;login:, January 1999 ![]() Mark Lutz <learning-python.com> Mark Lutz is a software developer, and a Python writer and trainer.He has worked on compilers, programming tools, scripting languages, andassorted client/server systems. If you're like most developers, one of the first questions you may askwhen you hear about a new programming language or tool is: what can Iuse it for? After all, programming languages are tools; although theycan be interesting to study in isolation, their real utility lies inthe context of applications. Python Defined In terms of acronyms, Python can be classified as both a VHLL (veryhigh level language) and an OODL (object-oriented dynamic language).Although this puts Python in the same general category as languagessuch as Smalltalk and Eiffel, Python has a flavor all its own. Forinstance, Python has a very practical orientation: unlike some OO(object-oriented) languages, Python was created by and for engineersinterested in solving day-to-day programming tasks. Because Python is a general-purpose language, arriving at a singleall-encompassing definition isn't an easy task. What Python is dependsmuch on how it is used. To some, Python serves as programmablefront-end to libraries coded in a compiled language like C or C++. Forothers, it takes the form of an embedded scripting language forcustomizing larger systems. And many programmers use Python as astandalone language, leveraging its library of precoded systeminterfaces. But in terms of basic functionality, Python can bedescribed in a variety of ways. Object-oriented Scripting LanguagePython is a fundamentally object-oriented language. Its "class" systemsupports modern OO development paradigms and makes Python ideal for usein OO systems. Perhaps more important for a scripting language,Python's object model is surprisingly easy to use, yet it supportsadvanced OO concepts such as operator overloading, multipleinheritance, and more. Standalone Rapid Development ToolPython provides both a conceptually simple language and rapiddevelopment-cycle turnaround. It is specifically optimized to supportspeed of development. In fact, many programmers find Python developmentto be orders of magnitude faster than other approaches. By combiningdevelopment speed with integration tools, Python fosters radicallyfaster development modes. Next-generation Extension LanguageLike many scripting languages, Python programs can be both extendedwith modules coded in a compiled language like C and embedded in a Cprogram. But unlike many scripting tools, Python provides bothintegration with external components and a powerful OO language.Because of this combination of utility, Python can address a wide rangeof problem domains. Freely Available, Interpreted LanguageBy most definitions, Python is an interpreted language. Moreaccurately, Python code is compiled to portable byte code that is runby a virtual machine. This scheme provides both fast turnaround afterprogram changes and reasonable execution speed. Finally, Python is truefreeware: its source code is freely available, and it may be embeddedin products without copyright constraints or fees. Some Quotable Quotes But don't take this writer's word for it. Some of the comments thathave been made by Python programmers on the Internet speak volumesabout the language's design. Although personal reactions vary, thefollowing quotes are representative of the level of enthusiasm Pythontypically generates in newcomers: "Python looks like it was designed, not accumulated." Pythonsports a refreshingly coherent design. Python's creator carefullybalanced the need to support practical programming needs with thedesire to avoid feature glut and complexity. The result is a simplelanguage that scales well to support larger systems. Moreover, Pythonhas a remarkably clear syntax: for many, its inherent readability makesit ideal for programs that may be reused or maintained by others. "Python bridges the gap between scripting languages and C."Python's design and implementation place it somewhere between compiledlanguages such as C and traditional scripting languages like csh, Awk,and Perl. Python's syntax resembles languages like C (e.g., there areno "$" variable prefixes), but its high-level programming tools andlack of compile and link steps are familiar to users of other scriptinglanguages. In some sense, Python provides the best of both worlds: itsupports scripting and embedding, but doesn't ask programmers toabandon normal standards of development quality. "Python is the BASIC of the 90s." Although Python is certainlybeing applied in real companies and products, many current Python usersfall into the "hobbyist" category: they use Python because they wantto, not because they have to. In fact, Python's popularity is largelydue to a strong grassroots following among engineers, similar in spiritto the support BASIC enjoyed in the early days of the PC revolution.Naturally, Python isn't a new and improved BASIC (it incorporatesadvanced programming tools such as exceptions, modules, and classes),but it generates similar excitement. "Python is as easy or as powerful as you want it to be." Pythonprograms can range in complexity from simple five-line scripts, tocomplex object-oriented frameworks. Program complexity can be scaledfor both the programmer's skill level and the problem at hand. Forinstance, Python supports OOP, but does not impose it: classes are anoptional language tool. Programmers (not language designers) decidewhether OOP, and other advanced Python tools, are warranted for eachtask. "Python: less filling, tastes great." This quote will probablybe more meaningful if you're familiar with American beeradvertisements, but it underscores one of the central concepts inPython. If you've used other compiled, strongly typed languages like Cor C++, you'll find that Python eliminates much of the complexityinherent in traditional tools. In fact, some have called Python"executable pseudocode": because of their simplicity, Python programscan more closely reflect the problems they are designed to solve. Some of these quotes will make more sense after we examine the languagein more detail. For instance, some of Python's simplicity stems fromits high-level built-in object types (lists, dictionaries, etc.) andthe absence of type and size declarations in Python (objects arecreated dynamically and reclaimed automatically when no longer used). A Python History Lesson Python was created by Guido van Rossum in Amsterdam around 1990. BeforePython, Guido worked on the ABC programming language and the Amoebadistributed operating-system and both of these influenced Python'sdesign. For instance, Python was originally conceived as a scriptinglanguage for the Amoeba system, and it inherits ABC's usabilitystudy-inspired syntax features. But Python adds practical features toABC, such as a C-like nature and integration with external Ccomponents. Python borrows features from a variety of languages,including Modula, C, Icon, and C++. Despite the proliferation of snake icons and book covers, Python isnamed after the BBC comedy series "Monty Python's Flying Circus."According to Python folklore, Guido viewed various Monty Python videosat the time he was busy inventing a new object-oriented language; theassociation naturally arose. This legacy tends to foster a generallyhumorous (and occasionally irreverent) flavor to the Python community.For instance, the standard labels "foo" and "bar" become "spam" and"eggs" in Python examples, and quotes from Monty Python skits pervadethe Python culture (and documents like this). Python was first offered to the public domain in 1991, and a USENETnewsgroup devoted to Python first appeared in 1994(<comp.lang.python>). Python now enjoys a growing internationaluser community, with a strong following in the U.S., Europe, Japan, andAustralia. A nonprofit support organization, the Python SoftwareActivity (PSA), has been established to help manage some of its growth.The PSA maintains Python's Internet sites and helps organize Pythonconferences. At this writing, the PSA is hosted by the Corporation forNational Research Initiatives (CNRI), which also pays Guido's salary. Uses for Python Python's general-purpose nature makes it applicable to more domainsthan I can list here. But as a sampling, here's a quick look at some ofthe most common things people are using Python for today. System Utilities (Shell Tools)Python's existing interfaces to operating system tools make it idealfor writing portable system utilities. Among other things, Pythonsupports UNIX tools such as sockets, regular expressions, and POSIXbindings. On MS-Windows, Python also interfaces with COM, OLE, andmore. Graphical User InterfacesPython's high-level nature and rapid turnaround are well suited to GUIdevelopment. It comes with an OO interface to the Tk GUI library, MFC,X11, and more. The Tk interface runs on X Windows, MS-Windows, and theMacintosh, making it a portable GUI solution. Internet ScriptingPython also comes with a full suite of Internet tools. It has supportfor CGI scripts, Web browser applets, Web agents and crawlers, HTMLgeneration, and more. Precoded library modules support most Internetprotocols: FTP, URLs, HTTP, email, etc. On MS-Windows systems, Pythoncan also be in conjunction with ActiveX scripting, and the newJPython system supports Python/Java integration. Database ProgrammingObject persistence is a standard part of Python: programmers can saveentire Python objects to a persistent store and retrieve them later.Moreover, Python offers interfaces to more traditional databasesystems, such as Oracle, Informix, Sybase, mSql, PostGres, and ODBC. Component IntegrationBecause Python can be integrated with components written in compiledlanguages like C and C++, many use Python as agluelanguage, tocontrol or extend application components. Wrapping libraries in ahigh-level language like Python, makes them easier to use. Andembedding Python in a C/C++ program, opens up the system tocustomization without recompilation. Rapid Application DevelopmentTo Python programs, components written in Python and C look the same.This allows developers to implement in Python first and later moveselected components to a compiled language like C. This developmentmode is often calledprototype-and-migrate: when used well, itcan leverage both the development speed of Python and the performanceadvantages of C. More Specific DomainsPython is also being used in more specific application areas, such asnumeric programming, artificial intelligence, image processing, anddistributed object systems (e.g., CORBA). Although these areinteresting applications, many are really instances of the componentintegration potential of Python. For example, Python numericprogramming is implemented as compiled-language libraries, integratedinto Python for ease of use. What Python Can't Be Used For Naturally, no language can be optimized for every development need.Like most interpreted languages, Python sacrifices some performance inorder to maximize development speed. Because of that, Python is not theideal tool for implementation of time-critical components. Forinstance, compute-intensive image processing libraries are probablybest coded in a compiled language such as C, C++, or FORTRAN. But even in performance-critical domains, we can make use of Python'sdevelopment speed. By implementing compute-intensive components in acompiled language and exporting them to Python, we get a system that'sboth easy to use and efficient. The numeric programming examplementioned previously is a perfect example of this approach todevelopment. Python is optimized for speed of development and designed formultiple-language systems: by adding it to the mix, we can make use ofa rapid development language for our system interface andinfrastructure while simultaneously achieving performance goals.Moreover, Python can be used to prototype demanding systems beforeinvesting the extra time needed to code critical components in C. Compulsory Features List Another way to understand Python is by listing its major features.Table 1 summarizes some of the things people seem to like best aboutthe language. It's by no means complete; the point to notice is thatPython combines a wide variety of features in a single, relativelysimple language. For many, the end result is a remarkably expressiveand responsive language, which can actually be fun to use. Table 1: Major Python Features
Note that most of Python's features aren't new by themselves: it'sreally how Python combines features that make it what it is. For many,it seems to be theright mix of programming language concepts. Python in the "Real World" Python is more than a collection of features. It has become a tool ofchoice for real programmers in real companies around the world. To geta feel for how companies apply Python for real development tasks,here's a sampling of some of the larger organizations in Python'scurrent user base. This list is necessarily incomplete; check Python'sWeb site <http://www.python.org> for up-to-date information aboutother companies applying Python. Surfing Made Easy (Infoseek, Four11)Many major Internet-related companies use Python in a variety of roles.For instance, Infoseek was an early adopter of Python in its Web-searchsystems. Cockroaches and Pepsi Jingles (Blue Sky Animation)If you've seen the movieJoe's Apartment, you've seen Python atwork. Python has been successfully applied as a scripting language forcreating commercial-grade animation. Blowing Up the Energizer Bunny (the Alice VR System)The University of Virginia uses Python as the scripting interface forits virtual reality system, Alice. One Alice demo features theEnergizer Bunny (along with methods to make it explode). Rocket Science (Lawrence Livermore Labs, NASA)Lawrence Livermore Labs use Python in numerical programmingapplications and sponsored a recent Python conference. Python is alsobeing applied in a number of ways at NASA. Linux Install Tools (Red Hat)Any time you install the Red Hat Linux system, you're using Python:portions of Red Hat's installation system are written in Python. Distributed Object Systems (ILU, Hector)ILU and Hector both make use of Python in distributed objectprogramming systems. ILU, from Xerox, is a CORBA-based system thatprovides bindings to Python's object model. Finding the Grail, Intelligent Agents (CNRI)Besides Python, Guido van Rossum also created an extensibleInternet/Web browser called (appropriately enough) Grail. It's writtenin Python, and allows Python programs to defineapplets executedin the client (browser), much like Java. CNRI also uses Python inresearch on intelligent agents for the Internet. Apples and Oranges and Bananas and Coconuts Python is often compared to other free (and not so free) languages,especially on Internet forums. Although language choice is often amatter of style, a brief comparison to similar tools can help point outboth some of Python's primary distinctions and some of the main reasonspeople choose to use it. Table 2 summarizes some of the most commonarguments of Python proponents. Naturally, your mileage may vary. Some of the distinctions in Table 2reflect different design goals. For instance, Perl is optimized fortext processing, C++ and Java are generally considered to be systemslanguages (not scripting tools), and Tcl is targeted more toward simpleglue tasks than larger systems development. Programmers matter, too: although Python is deliberately designed toprevent unreadable code, there's nothing stopping us from writing it ifwe try. Moreover, having many languages to choose from can only be agood thing. In fact, support for multiple-language systems is one ofthe central concepts in Python: no language can satisfy all our needsat once. Programmers must be empowered to pick and choose the best language foreach task at hand. Python is a great language to have in yourdevelopment toolbox, but it should not be the only one. Table 2: Python versus similar tools
For More Information To learn more about Python, visit Python's Web site<http://www.python.org>, the author's Web site<http://learning-python.com>, or the Python USENET newsgroup<comp.lang.python>. Besides a vigorous online community, you'll find tutorials, referencemanuals, and Python books. For instance, at this writing, O'Reilly<http://www.oreilly.com> publishes two Python books, with a thirdon the way. |