Python Ben Hester csc 415 November 2012 a look at Python



Download 57.28 Kb.
Date09.01.2017
Size57.28 Kb.
#8176
Python

Ben Hester

CSC 415

November 2012



A look at Python

What is Python?

Python is an interpreted object-oriented programming language. It incorporates such

features as dynamic typing and high level dynamic data types and classes. The language is

known for having a simple and easy to learn syntax but is also very powerful. Python has

extensive libraries on its own but can be extended even further using C or C++. Since

Python is able to be extended with C or C++ a developer could use C++ in their Python program

to speed up certain sections of code or have some portion of an algorithm closed off. Python is

also very portable and has been adapted to run on number of devices. Each of the major

operating systems (Windows, Mac OS, and Linux) is capable of running it as well as many

mobile devices such as Palm OS, iPhone, Android and much more.

Because of its versatility Python has become the preferred language for many developers

including famed programmer Eric S. Raymond who coined the phrase “open source”. It is seen

as a teaching language as well as a highly functional one. Although Python has picked up a

sizable following, it is still not as common as the big three languages C++, Java and C#. Still

Python is growing in popularity with major businesses such as Google and could rise up to



become one of the major languages used in the future.
History of Python

Python was created in 1989 by Guido Van Rossum and gets its name from the BBC show

Monty Python and the flying circus. Van Rossum started the language as a side project while

working at the National Research Institute for Mathematics and Computer Science in the

Netherlands. He was looking to build a successor to the ABC programming language. The idea

behind Python was to create a scripting language with all of ABC’s best features but

without any of its problems. In 1991 Python was published by the National Institute of

Mathematics and Computer Science as open source. In 2001 the Python Software Foundation

(PSF) was formed, which is a non-profit organization created specifically to own Python-related

intellectual property. Van Rossum is still the principle author of Python although other

programmers have contributed to the language. All of Pythons official updates are controlled by

PSF, but Python remains open source.

Python is still being improved by the programming community and by PSF. In 2000

Python 2.0 was released which added a new fundamental data type called UNICODE strings,

which was the largest feature added in this build. In 3.0, this changed when the data types were

split between text and binary data. Build 3.0 was also the first backwards compatible Python

build. As of this writing the newest version of Python is 3.3 which reworked the I/O exception

hierarchy.

As stated before, while Python is not the most widely used language, it is being

implemented by major companies such as Google for its infrastructure. Game developers such as

Fixaxis Games used Python in creating their popular game Civilization 4. The highly successful

data cloud service Dropbox was created in Python. There is an increasing amount of big name

companies and successful up and coming companies that are using Python for their products.

While Python has gained use in the private sector, it still is being used as a highly successful

education tool. In 1999 Van Russom summated a proposal called Computer Programming for

everyone or CP4E which was accepted and received a grant from DARPA. The proposal detailed

the use of Python in high schools and colleges to teach basic programming. While the official

program is still in limbo, colleges across the country have started to use Python as a teaching

language on their own due to Python’s simple syntax. Python users have helped the education

initiative by creating their own high quality educational material for Python. A free online ebook

called “a byte of Python” by Swaroop CH has been used as educational material by NASA and

by Harvard University. Python has no shortage of free quality educational material.
Details of Python

Names, Bindings, and Scopes

In Python names are case sensitive. Meaning the variable name “Count” is not the same

as “count”, they are considered to be two different variable names. Variable names in Python

don’t have a variable length limit so the variable names are only limited in size by how much

memory the machine that you are developing on has. Names must begin with a letter or an

underscore. After that names can consist of letters, numbers, or UNICODE characters such as

“<”.

In Java or C#, when a variable is declared, a type must be given to that variable. However

Python uses what is called “dynamic type binding”. This means that the type of variable is not

set when the variable is declared. The type is set after a value has been assigned to it. For

example the variable “count” could be a string, float, or an integer when it’s first declared but

would be considered an integer after “count” is assigned the value “4”. Dynamic type binding

does have some disadvantages. It increases the run time of a program since type checking has to

be done at execution. The interpreter must also keep a run-time descriptor for every variable in

memory to keep track of the variables type. The size of the storage for a variable must also be

dynamic since different types of data require different storage sizes. The way Python handles

memory is by putting each of the variables onto a heap which is controlled by Pythons memory

manager. Python’s memory manager controls the allocation and deallocation of variables in

memory.

Even though the variables are set dynamically, Python does make it easier for a developer

to identify the type of a variable before execution thanks to static scoping. Static scoping means

that before the execution of a program, a variable’s scope has already been determined. This

allows the type of a variable to be determined at compile time by a programmer or a compiler.

However, static scoping is handled a little different in Python than in C-based languages. Python

allows for nested subprograms. This allows a programmer to have a function that can only be

accessed by a parent function. A parent function is able to access any of its nested functions and

any of the variables contained within those nested functions. Python also allows global scoping.

This means that a function can access and change a variable that is declared outside of any

functions. However if a function needs to change a global variable then the variable must be

declared global within the function that it is called.
Data Types

As stated earlier, Python uses dynamic typing which means that variables don’t have a

type until a value is assigned to it. Each of the variables are put into a heap and handled by

Pythons memory manager. Some of the ways Python handles those variables is different from

most languages.

Python uses unlimited sized integers. When an exceptional long integer is assigned to a

variable the type becomes a “long integer” which is only limited in size by the address size in the

machine. Python also supports complex data types which are represented as ordered pairs of

floating point numbers. The imaginary part of a complex literal is specified by

following it with a “j” or “J”. Strings in Python are considered a primitive type but made up of

character arrays. The string length is static and is set when the string is created. Due to strings

being made up of character arrays, manipulating strings is easier to do using regular expression

commands, catenation and indexing specific characters. Python uses its built in “re” module for

regular expressions which give it powerful capabilities similar to PERL. Another feature

concerning variables is that there are no pointers in Python. Each of the variables are references

to either objects or values in memory.

In Python arrays are called lists which function as dynamic arrays. As with C-based

languages the elements within the arrays must be the same type, however in Python these

elements can reference objects or different data values. This means that arrays in Python are

heterogeneous and can consist of references to different data types within the same array. Python

supports many different tools for working with and structuring arrays. Arrays can be broken up

into slices which allow a developer to take only the sections out of array they need. An example

of a complex slice would be selecting only every other element in an array. Another tool is

support for associative arrays. This allows a developer to designate a key for each element within

an array. Python also allows for array catenation and decreasing array size during runtime but

both of these actions have to be carried out with methods.

In addition to arrays, Python supports tuples. Tuples are similar to records except the

elements are not named. Tuples in Python are immutable but they can be converted into arrays

and back to tuples again if a change needs to be made. The benefit of tuples is when a

programmer doesn’t want a set of data to be changed by a function. This allows a set of data to

be sent to a function as a parameter without worrying if the function will change the data.

Expressions and Assignment Statements

Python is fairly standard when it comes to expressions and assignment statements.

However there are a few details to note. First of all Python uses the same order of precedence

that most C-based languages use. Python also allows for operator overloading though special

named methods that begin and end with an underscore. Instances of the class containing these

methods would be called to carry out the operation. For logical operations, every one of them in

Python is short-circuited. This means that the results can be obtained from an expression without

evaluating all of the operators. As for assignment statements, since each data type is associated

with an object and not a variable there is no such thing as mixed-mode assignments in Python.

Statement­Level Control Structures

One of Python’s most noticeable features comes from the way it handles control

structures. Instead of parentheses and curly brackets, Python uses indention for its control

statements. This lends to Pythons easy to read syntax because it forces clear code and removes

the need of finding a missing parentheses or curly bracket. Another of Python’s control structure

features is the ability to use nested selectors. This allows a programmer to develop using nested

if statements. Unfortunately, Python doesn’t support multiple-selection statements. This means

tools such as switch statements are not supported in Python. Python further improves readability

by not including support for “goto” statements. While convenient to use these statements make it

much harder for other programmers to debug or rewrite code that features to many goto

statements. It is considered to be a bad practice to use them.

For-loops are handled a little differently in Python. Instead of the typical format found in

most languages (for int i= 0; i

works similar to a for-each loop found in other languages. To do a simple counting loop the

range() function is needed. The programmer would define a range for the for-loop to count

through as each step is completed. Below is an example of a simple counting loop starting a 0

and going to 4.
For count in range(5):

Print count


This block of code produces [0,1,2,3,4]. A range can also be set to begin and end at certain

integers such as range(2,6) which would start the count at 2 and go to 5 because the range

function never reaches the last integer for a range.
Subprograms

Subprograms in Python are declared with the def header followed by the functions name

and then any parameters for the function. Every function in Python returns a value, even ones

that are not set to return a value. In those cases the value “none” is returned. This is usually

ignored by the interpreter but can be seen through a print statement.

One of the features that sets Python apart from most of the other common languages is

that subprograms are executable. Whenever a def statement is executed, the given name is

assigned to the given function body. Until the def statement is executed, the function cannot be

called. For example, you can have two different versions of the function fun nested in an if –

statement. Each one can be set up with completely different parameters and functionality. Based

upon which clause of the if-statement is executed, determines which version of the fun function

is used and which one can now be called. Python also allows the use of nested subprograms. This

means that a subprogram can have its own subprogram contained within it. The benefit from this

is that it provides a highly structured way of granting access to nonlocal variables in the enclosed

subprograms.

As with almost every other language Python uses positions to correspond between formal

parameters and actual parameters. In addition to position parameters Python allows keyword

parameters. With keyword parameters the order doesn’t matter and each of the parameters

passed are assigned to each of the formal parameters of the function. The benefit of keyword

parameters is when a programmer needs to use a function with a long parameter list. This insures

that a mistake with the position cannot occur since each of the parameters is directly assigned.

Python also allows default values in formal parameters. In the event that a value is not passed

into a parameter that has a default value, the function is executed with that default value.

Python has a fairly unique way of passing parameters. Unlike in most other languages,

Python doesn’t do type checking on the parameters being passed. This is because everything in

Python is an object. While objects have types, variables do not. The method Python uses is called

pass-by-assignment. Since all of the data values in Python are objects, every variable is a

reference to an object. The actual parameter value is assigned to the formal parameter. Pass-by-

assignment is basically the same as pass-by-reference because all of the actual parameters are

references.


Abstract Data Types and Encapsulation Constructs

Python doesn’t support encapsulation. Although you can declare a semi private function by using an underscore before the function name. However this isn’t a private function in a traditional sense and doesn’t stop the function from being accessed directly from a different class. It only prevents other classes from accessing it indirectly.


Object-Oriented Programming

Python is a primarily an object-oriented language despite the lack of encapsulation. Everything in Python is considered an object. This includes every class, function, and variable. One of Python’s key object-orientation features is multiple inheritance. This allows a class to inherit traits from multiple different classes. Another one of Pythons object-orientated features is the way Python handles polymorphism. Instead of using traditional polymorphism, Python uses what is called duck typing. The operator “is” tests an objects identity in order to figure out what kind of object it is. Pythons method changes based on the type of object being given to it.


Concurrency

Since Python is an interpreted language the only type of concurrency that can be used is logical concurrency. This means that a python program cannot directly take advantage of a machine that has multiple processors. However, Python does allow a programmer to create threads in Python. These threads can carry out tasks independently, adding concurrency to Python programs.


Exception Handling and Event Handling

Just like what is found in many languages, Python also supports exception handling. Instead of a Try..Catch block, in Python it is a Try..Except block but it works exactly the same. In addition to the standard catch block Python provides additional tools for working with exceptions. The raise statement allows a programmer to create an exception object in their program that will either re-raise the last exception within the current scope or if no exception is found in the current scope a runtime error is raised. This function is helpful for troubleshooting problems within sections of code. Python also allows users to create their own exceptions through class objects. However it is recommended that these new exceptions be derived from Pythons built-in exception class.

Python really doesn’t have built in event handling options other than the raw_input function. However Python does have a great deal of modules available for event handling. One of these modules is called tKinter. This is a GUI module that also provides event handlers for a Python programs GUI interface. It also seems to be the most popular.
Other Python Features

Python has two modes, programmer mode and interactive mode. In programmer mode the finished .py files are run as normal in the Python interpreter. Interactive mode is a command line shell that gives immediate feedback for each statement as it goes through memory. In this mode any code you type is immediately run. This mode is useful for troubleshooting issues with a program.


Evaluation of Python

Readability

Readability is probably Pythons strongest feature. Python is well known as an easy to understand language. This is why many teachers use it as a introduction language. Pythons greatest strength is in its simplicity. Python forces a programmer to use indentions for their control structures and the lack of a goto function ensures the code will be easier to read.
Writability

Python excels at writability as well, although this is diminished by the lack of built in event handlers. Due to the lack of built in event handlers, a programmer will either have to learn an existing module or create his own. Overall Python is easy to write with its simple syntax. Python makes it easy for programs to add their own classes or packages to Python. Python can also use C or C++ packages which makes it easier for programmers to implement code they find in those languages.


Reliability

While Python has many features making it reliable, I found the language lacking in this aspect. Python doesn’t do type checking when passing parameters, doesn’t support encapsulation, and uses aliasing. While the readability/writability helps insure there are fewer errors in the code, the lack of certain features lowers it reliability.


Cost

One of Pythons key features is portability which makes it very cost effective. Python is open source and easy to learn. C or C++ programmers will already be able to make packages for Python. However the cost is increased with the lack of encapsulation or native event handlers increases cost. Python is a scripting language, which means the memory manager must always keep track of the type for each of the variables which slows the code down and makes it not as fast as C code. This is why many programmers use C packages for certain sections of their program to speed up performance. Overall Python is very flexible and easy to work with but without using C code packages, wouldn’t be the best language to use for real-time results or heavy processing.

Works Cited

Beazley, David. "An Introduction to Python Concurrency." An Introduction to Python Concurrency. USENIX Technical Conference, June 2009. Web. 20 Nov. 2012. .

"Learnpython.org." Learn Python. Learnpython.org, n.d. Web. 01 Oct. 2012. .

Lott, Steven. "Python - Raising Exceptions." Python - Raising Exceptions. Linuxtopia.org, 2002. Web. 20 Nov. 2012. .

"Python V3.3.0 Documentation." Overview. Python Software Foundation, n.d. Web. 01 Oct. 2012. .

Sebesta, Robert W. Concepts of Programming Languages. Boston: Pearson, 2012. Print.

Shaw, Zed A. "Learn Python The Hard Way, 2nd Edition." Learn Python The Hard Way, 2nd Edition — Learn Python The Hard Way, 2nd Edition. N.p., n.d. Web. 01 Oct. 2012. .

Swaroop, CH. A Byte of Python. Vol. 1.9. N.p.: n.p., 2008. Print.



Vrajitoru, Dana. "C311 Name, Scope, Binding." C311 Name, Scope, Binding. IUSB, n.d. Web. 01 Oct. 2012. .

Download 57.28 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page