Python

advertisement
Python
CSC 415: Programming Languages
Jonathan Huelman
11/8/2011
History of Python
Development
By the 1980s, programming had become a distinct technical field. New languages were
being created and implemented to use conventions defined in the previous decades. Instead of
coming up with new paradigms for imperative languages, developers were designing new and
effective elaborations on the concepts set forth by the earliest languages. C with classes (later
known simply as C++) was breaking ground in object-orientation, and Modula, Ada, and ML
were developing note-worthy module systems to be used for large-scale organization.
In 1983, a man named Guido van Rossum landed a job at Centrum voor Wiskunde en
Informatica (CWI), the national research center for mathematics and computer science in the
Netherlands. Van Rossum was working as an implementer on a team that was creating a
language called ABC. Design and development of the language had started in the late 1970s and
their focus was clear: they envisioned a programming language that could be taught to any
intelligent computer user who was not necessarily a programmer or a software developer. Their
main audience was the science-minded community, of course. Unfortunately for the ABC team
at CWI, the language never really got off of its feet. Van Rossum himself reasons that “Maybe it
was too early, before there was an Internet to do efficient distribution.” (Venners)
After working on ABC, in 1986, van Rossum worked on a distributed operating system
known as Amoeba. In his development of the OS, he discovered that he would need a scripting
language. Based on his experience with ABC, using the parts he liked and discarding the things
he was never comfortable with, van Rossum structured the scripting language to be easily
extendable, include modules, and to be easy to read and write.
It was in February 1991 that Guido van Rossum published the code as version 0.9.0 of
Python, a new programming language that he had named after the famous British sketch comedy
series, Monty Python’s Flying Circus. With the creation of a primary discussion forum in 1994
(alongside the release of Python 1.0), Python’s userbase grew exponentially, putting this littleknown high-level scripting language on the map for the first time.
By 1995, when van Rossum earned a job at the Corporation for National Research
Initiatives (CNRI) in Reston, Virginia, there had already been Python 1.1 and 1.2. While at
CNRI, van Rossum released several more versions, adding features such as keyword arguments
and built-in support for complex numbers. It seemed that his initial goal, to “bridge the gap
between shell and C” (Venners, part II), was being reached tenfold.
With the advent of the Internet, distribution became easier and writing implementations of
all of the protocols available could be done purely in Python. What van Rossum originally
intended to be a short-term, small-program-writing language began to be used for programs 500
lines and longer, and as people began to write their own modules, Python set the stage for what
would become a large-scale user-defined platform with thousands upon thousands of resources
available.
From BeOpen.com, where the Python development team moved in 2000 and became
PythonLabs, they released Version 2.0, a massive undertaking that included support for list
comprehension, garbage collection, and in 2.2, Python’s types and classes were unified, making
Python an entirely object-oriented language.
December 8, 2008 marked a change in Python— not only in features, but in design and
philosophy. Because Python had accumulated so many redundant ways to do the same thing, the
newly released Python 3.0 focused on getting rid of all the duplicate constructs and modules, all
in an effort to keep its overall design philosophy intact. Unlike Perl, the open-source high-level
language designed by Larry Wall, who propagates that “there is more than one way to do it,”
Python sticks to a rigid idea that “There should be one- and preferably only one- obvious way to
do it.”
Results
Since the release of Version 2.0, Python has quickly become the preferred programming
language of many famous and important companies and development teams. On the official
Python website, python.org, there is a “Quotes” page where certain industry leaders sound off
about how much they use and enjoy Python.
Cuong Do, a software architect at YouTube.com, says “Python is fast enough for our site
and allows us to produce maintainable features in record times, with a minimum of developers,”
and Peter Norvig, the director of search quality at Google, Inc., explains that “Python has been
an important part of Google since the beginning, and remains so as the system grows and
evolves. Today dozens of Google engineers use Python, and we're looking for more people with
skills in this language.” (python.org)
To say that Python has surpassed its original design is beyond understatement. What began
as a simple scripting language for use in an operating system aimed at people who were
unfamiliar with the ins and outs of programming has become a multifaceted, user-supported,
ingeniously basic programming language all its own, with users projected to number in the tens
of thousands (python.org). Python has stepped forward as one of the most useful, flexible, and
broad technical tools of this century, which is not to say that it is alone. In the world of computer
science today, with over 1,300 free-to-use specific languages available over the Internet, Python
certainly has a lot of competition, but the whole focus of the language, which is to make
programming as simple and non-specific as possible, seems to be diverting away from the notion
of competition between languages in favor of cooperation and interdependence.
Overview of Design, Syntax, and Semantics
Influences of Other Languages
Because he was creating Python amidst a large boom of other programming languages,
Guido van Rossum’s approach to developing Python was to take the things he liked and to leave
behind the things that he did not like. There were, of course, many similarities to ABC, but the
differences were notable: “Python's lists, dictionaries, basic statements and use of indentation
differed from what ABC had,” van Rossum comments, and also in much simpler cases such as
deciding syntax: “ABC used uppercase for keywords. I never got comfortable with the
uppercase, neither reading nor typing it, so in Python keywords were lowercase.” (Venners, part
I)
Python’s syntax was originally designed to be high-level— all keyword, data, and control
statement constructs would be as easy to distinguish and learn as possible. Again, Guido van
Rossum wanted to improve on ABC’s design. “ABC had no concept of a standard library. It had
built-in commands, but the parser knew about them. It had built-in functions that were very
much integrated in the runtime. Adding to the language's standard functionality was very
difficult,” he explained to Bill Venners.
It is important to know two things about Python. First, it is strictly an interpreted language,
and second, there are thousands of components in its standard library that allow it to do
everything a scripting language should do in the one way that has been deemed best. This makes
it very powerful and very quick to learn. Eric Raymond writes in his April 2000 article, “Why
Python?”: “My second [surprise] came a couple of hours into the project, when I noticed
(allowing for pauses needed to look up new features in Programming Python) I was generating
working code nearly as fast as I could type. When I realized this, I was quite startled.” He then
explains that sometimes programmers get a little ahead of themselves in coding, only to realize
later that what they are asking is impossible, and when this does not happen to you a lot when
you first start with a language, it feels like you’ve mastered it immediately.
The fact that Python is interpreted makes it possible to have a simple command-line
window open up (called “python”) where one can type commands in and have the interpreter put
them together as you type, instead of having to type a whole program, compile it, and run it. For
example, typing in
>>> the_world_is_flat = 1
>>> if the_world_is_flat:
...
print "Be careful not to fall off!"
...
Produces the output
Be careful not to fall off!
Showing that control statements can even be evaluated using the “...” continuation line indicator
(which you can produce by pressing Shift + Enter).
Names, Bindings, and Scope
Names (or identifiers) in Python follow a pretty general schema: It must start with a letter
or an underscore ( _ ) and the rest can have any combination of letters, digits, or underscores,
with unlimited length and the case being significant, so that sum, Sum, SUM, and SuM are
three different variables. Keywords are also reserved words, and they cannot be used anywhere
in Python except for the context for which they were intended. Some examples are def, and,
in, import, is, lambda, and yield.
Names actually refer to objects, as every defined function or procedure in Python is an
object. Names are introduced by binding operations in blocks. Blocks are the pieces of Python
code that are executed as a unit. Each interactive command, script file, and script command is a
block by itself.
The scope of Python is very basic. If a local variable is defined, its scope is the block in
which it was defined. If the definition is within a function block, the scope is any other block
inside the one that defined it (unless of course a new binding is given for it somewhere in there),
and all variables bound at the module level are global. Variables used in code blocks where they
were not defined are called free variables.
Names are bound in many ways. Formal parameters, import statements, class and function
definitions, and targets that are identifiers in assignments, for-loops, and more.
(python.org)
Data Types
Python has been defined as duck-typed, which means that variables have types based on the
data that is assigned to them. It is also strongly typed, meaning there is no chance for interaction
between clashing types without casting and converting, and it is dynamically typed, because the
majority of its type checking is performed at run time, not compile time.
Guido van Rossum has expressed his dismay at having to choose between making Python
statically typed or dynamically typed, because, he says, it is not always as simple as “static-
typing bad, dynamic-typing good.” He expects that the future holds a sort of mix between the
two, allowing the GUI and I/O that works so well with dynamic typing to work in static as well.
“A hybrid language with functional and dynamic aspects might be interesting,”
van Rossum says, “I should add that despite Python’s support for some functional tools like
map() and lambda, Python does not have a functional-language subset: there is no type
inferencing, and no opportunity for parallelization.” (Biancuzzi and Warden)
Variables can be declared anywhere, anytime, and altered however the programmer sees fit.
If needed as a certain data type, Python will likely know what you mean and make it fit.
Variables can also be declared all at once, for example
>>>
>>>
0
>>>
0
>>>
0
x = y = z = 0
x
y
z
Python has support for all the regular numeric data types, including integers, floating-point
decimals, and even complex numbers. There are plain integers, (in the range -2147483648
through 2147483647), long integers (in unlimited range, subject only to available memory), and
Booleans, which can only be 0 (False) or 1 (True).
However, Python sees no need for single-precision floating-point numbers, because the
overhead of using objects in Python greatly outweighs the savings in memory single-precision
gives. There is only one floating-point type available, float, and it is double-precision.
Each complex number that is defined is done so with two floats as arguments, the first is
the real part, and the second is the imaginary part, called later with .real and .imag.
Python also includes a library of sequence types, that is, all the data types that represent
finite ordered sets with indices. All sequences have a built-in len() function which returns the
length, and each individual item is selected with square brackets. Sequences are further divided
into groups according to their mutability; their ability to be changed or not.
The immutable sequences, which cannot be changed once created, are strings, unicode
objects, and tuples. The mutable sequences are lists and byte arrays. A list is like a record in Ada
(any number of any types can be put in), and a tuple is like a list, only once it has been declared
it cannot be changed.
One exciting feature of Python is that all the sequence types support slicing. This means
that a colon can be used in the subscript to denote which elements of the sequence are to be
selected. For example, if you have a list called a:
a[i:j]
will select all items with index k such that i <= k < j. When used as an expression, a slice is a
sequence of the same type. This implies that the index set is renumbered so that it starts at 0.
Also included in Python are set types, which represent finite, unordered sets of unique
objects. They are not indexed by subscripts, but can be iterated over, as the len() function still
returns the number of items. Sets are mutable, but frozen sets are immutable. Frozen sets,
however, are also hashable, meaning they can be re-used as elements of other sets or as
dictionary keys.
The last type in Python that is worth mentioning (besides, of course, callable types (types
defined by the user)) is the dictionary. Described as a mapping type on the Python website,
dictionaries are very powerful tools that are very much like associative arrays in other languages
and hashes in Perl. It is easy to think of them as a set of key: value pairs wherein when the key is
referenced, the value is returned, and vice-versa.
(python.org)
Expressions and Assignment Statements
In terms of expressions and assignment, Python is actually very basic. All of the logical and
arithmetic operators of C++ are present, in addition to the ** operator, which does
exponentiation on-the-fly. All of the other numeric arithmetic operators (+, -, *, /, %,
abs()) are there, plus the Python-exclusive “//”, which returns the floor division of the
operands. At first glance, this might seem a little extraneous, even silly, but in his blog “The
History of Python,” Guido van Rossum himself explains his reasoning behind this operator:
“The integer division operation (//) and its sibling, the modulo operation (%), go
together and satisfy a nice mathematical relationship (all variables are integers):
a/b = q with remainder r such that
b*q + r = a and 0 <= r < b (assuming a and b are >= 0).” (van Rossum)
The most satisfying thing about Python as a scripting language is its support for dealing
with regular expressions, with syntax very close to Perl and Linux shell commands. As an
example, consider
import re
for test_string in ['555-1212', 'ILL-EGAL']:
if re.match(r'^\d{3}-\d{4}$', test_string):
print test_string, 'is a valid US local phone number'
else:
print test_string, 'rejected'
This code demonstrates how easy it is to use regular expressions in Python. It is a simple
loop that determines whether or not two test strings are in the format XXX-XXXX, where the
X’s are digits.
Statement-Level Control Structures
Python has the conventional if, while, for, and with statements, but the syntax
is a little different. In Python, after putting in the condition, the user must put a colon and have
all the following statements indented to be included in the statement.
Python’s for-statement is also a little different, and more comparable to the foreach
statement of Perl. The general form of Python’s for is:
for loop_variable in object:
- loop body
[else: or elif:
- else clause]
and is generally used to iterate over an object, but one can also use the range() function to
iterate over a series of numbers, as in traditional programming languages. (Sebesta)
Subprograms
Technically, all of Python’s procedures and functions are subprograms. Any of the wide
variety of built-in classes in the Standard Library can be used anywhere else, as well as any userdefined or third-party classes. The simplicity of Python is inherent in this design, as there is no
need for a main function or even separate “driver” programs, because everything can be fed
through the interpreter and then used as it is needed.
Support for Object-Oriented Programming
Part of the genius of Python is that, in effect, everything is an object. Python is structured
as an object-oriented language, so it provides built-in constructs that make it simple for
developers to structure code for maximum reusability. The number of Python’s constructs
number very high, and there is a system of forums and programs like PEP for deciding what to
add to Python over the various version releases. In order to understand the way Python works
with object-orientation, one must realize that the basic function definition, def, is defining
whatever you code in as a class: something that can be used and re-used as many times as you
like.
Concurrency
Python is a language designed with the problem of concurrency in mind. With packages for
both multiprocessing and threading (python.org), and even a package called
concurrent.futures in Python 3.2 that handles asynchronous callables. As with any
system trying to employ concurrency, Python is limited by the hardware it is being used on.
Exception Handling and Event Handling
Like most programming languages, Python deals with exceptions by way of a try block.
The try..except block in Python works as follows (python.org):

First, the try clause (the statement(s) between the try and except
keywords) is executed.

If no exception occurs, the except clause is skipped and execution of the
try statement is finished.

If an exception occurs during execution of the try clause, the rest of the
clause is skipped. Then if its type matches the exception named after the
except keyword, the except clause is executed, and then execution
continues after the try statement.

If an exception occurs which does not match the exception named in the
except clause, it is passed on to outer try statements; if no handler is
found, it is an unhandled exception and execution stops with a message as
shown above.
Event handling in Python can be done by the user in any way they see fit.
Other Issues of Python
Python is huge, almost overwhelmingly so. There are so many modules and extra things;
hundreds of functions are listed in the python.org Standard Library, it can be dizzying.
Depending on what you want to do and how you want to do it, Python most likely has support
for it.
Want to use random numbers? No problem. Want to serialize your objects? Try pickle.
Python has packages for five different compression types, twelve for file and directory access,
seventeen for generic operating system services, and tons more. It feels almost infinite.
Evaluation of Python
The last section of this report will be an evaluation of Python as a programming language
overall. Based on Sebesta’s four criteria of program language evaluation (readability, writability,
reliability, and cost), this evaluation will try to determine the pros and cons of Python.
Readability
At first glance, Python seems a little weird. The syntax is all okay; everything is either C++
or English understandable, but there is something missing: braces. For block comments, Python
uses a strict whitespace with indents and dedents. In his article titled “Why Python?” on how he
came to choose Python over Perl, Eric Raymond writes “I immediately tripped over the first odd
feature of Python that everyone notices: the fact that whitespace (indentation) is actually
significant in the language syntax. The language has no analog of the C and Perl brace syntax;
instead, changes in indentation delimit statement groups. And, like most hackers on first
realizing this fact, I recoiled in reflexive disgust.”
It certainly struck this evaluator at the beginning. But, as Raymond later concedes, after
about twenty minutes with it, using indentation in block comments seems natural, making
everything smooth and pretty. It is, in fact, very easy to read and understand, unless you have to
nest a bunch of loops and ifs. Even then, however, simple, pragmatic line-by-line scrutiny makes
everything clear.
Writability
Python is certainly readable only to the degree that it is writable. One cannot hope to
understand or be able to write anything more complex than standard programming without
thorough study of the documentation of the Python website. That being said, the documentation
is superb, showing concrete examples and proving the lasting epithet that “there should be oneand preferably only one- way to do it.” Without room to try and find different ways to mess with
your program only to have it do the same thing, programmers get their work done more quickly
and do not have to spend time worrying about strange, unintelligible errors coming out of
nowhere.
Reliability
Judging by the gleaming reviews all over the Internet, one could say that Python surpasses
all expectations in terms of reliability. Being as intensely reusable as all of the code you write is,
Python is certainly designed to last. With every new version, only a few things ever get replaced;
the developers opt to keep old things as long as they can while plugging the new features as
better and easier to use. One example is the “not equal to” operator, which, in version 1.0, was
simply “<>” like in other languages at the time. Nowadays, Python recommends the use of the
“!=” syntax, noting that the “<>” still works, but is obsolete.
The lifespan of Python is seemingly infinite. Barely thirty years old, the language has taken
off splendidly and remains in use by tens of thousands of people today. Instead of replacing Perl
or any other of the advanced scripting languages, Python has taken a seat as a very important-tolearn language, with more and more companies and employers demanding at least basic
knowledge of it from their employees.
Cost
The cost of any programming language is not determined solely by the cost to use it
(Python is Open Source— free to use— but some of the more advanced IDEs and other
development environments do come at a fee), but by the costs of training and / or finding
experienced personnel, and the costs of supplying hardware to support the language. Being so
reliable, Python is naturally very cost-effective. It is easy to teach, and easy to learn.
As long as companies are not interested in “Python masters—“ people who know every in
and out of the syntax and libraries— the cost of training could be very low. Companies can
decide for themselves which features of this extendable, ever-expanding language are going to be
used, and train their employees to these specific designs.
Finding people who already know Python is rapidly becoming easier and easier. As many
academic institutions transfer over to Python as their first language learned, and even more
people are learning it on their own or with the help of any of the numerous aids, Python is
quickly becoming a lingua franca of programming. Soon everyone will know Python as well as
C or Java, and this element of cost will be a moot point.
Overall
The most important criterion of those mentioned is up to employers, obviously, but with all
of them being in such good form, Python certainly seems like a top choice. It is, all at once,
conventional yet creative, modern yet rooted in history, and fun-to-use yet very seriously
integrated into every system imaginable.
The age of Python has begun, and we as programmers would do well to learn it and love it
as quickly as possible.
BIBLIOGRAPHY
Biancuzzi, Federico and Warden, Shane, ed. Masterminds of Programming. Sebastopol,
California: O’Reilly. 2009
Python Software Foundation. Python Programming Language. <www.python.org>
Venners, Bill. “The Making of Python, Parts I, II, and III” www.artima.com. January, 2003.
September, October 2011. <http://www.artima.com/intv/pythonP.html>
Raymond, Eric. “Why Python?” linuxjounral.com. 30 April 2000. October 2011.
<http://www.linuxjournal.com/article/3882?page=0,0>
Van Rossum, Guido. “Why Python's Integer Division Floors”. The History of Python. pythonhistory.blogspot.com. 24 August 2010. October 2011. <http://pythonhistory.blogspot.com/2010/08/why-pythons-integer-division-floors.html>
Sebesta, Robert W. Concepts of Programming Languages. Ninth Edition. Reading, MA:
Addison-Wesley
Download