Python for everything?

Mike Meyer mwm at mired.org
Thu Jun 30 18:53:16 EDT 2005


xeys_00 at yahoo.com writes:

> I posted a article earlier pertaining programming for my boss. Now I am
> gonna ask a question about programming for myself. I just finished my
> first C++ Class. Next semester is a class on encryption(and it's
> probably gonna be a math class too). And finally back in programming in
> the fall with C++ and Java 1. The C++ will cover pointers, and linked
> lists, sorting algorithms, etc... I run linux and OS X. I have read in
> the old days that C was used for everything. It was a systems
> programming language, and also did a lot of the same stuff Bash scripts
> and perl do now. So, in that era, C did it all, from short to tall. My
> question is, can Python "do it all"? I am wondering what to learn as my
> scripting language. I have read that perl is good up to about 250
> lines, and after that it gets kind of hairy. However, from what little
> I have heard about Python, it's very well suited for readability due to
> the whitespace requirements of the language, and very good for large
> projects due to it's large amount of modules and it's object oriented
> structure. I would like opinions as to the suitability of Python as a
> general purpose language for programming unix, everything from short
> scripts to muds. Thanks for your patience, opinions, and comments.

As other have noted, C was never really used for everything. Unix
tools were designed to connect together from the very beginning, which
is what makes shell scripting so powerful. This was true before there
was a C. Likewise, some things you need more control over the machine
than you get in C - those are still done in assembler. These days, C
compilers let you embed assembler statements in your C, so some of
these things are done in such variants.

Some things require you to be able to generate machine code. Those
can't be done in any current implementation of Python. That doesn't
mean they'll never be doable in Python - just not now. As mentioned,
some things needvery explicit control over the generated machine
code. Those things aren't done in C now, and will never be done in C
or Python.

Rather than talk about how suitable Python is for programming in
general, I wanted to talk about the things you mentioned you'd be
learning.

Python doesn't have explicit pointers - except that every name is a
pointer. That makes it hard to learn about pointers with Python. You
can do data structures in Python, though. Just replace the C/C++
struct with a class, and manipulate the attributes like you'd
manipulate struct members. For instance, a linked list might be
represented by:

    class ListNode:
        def __init__(self, data):
            self.data = data
            self.next = None

A routine that manipulated a list might look like:

   def cons(car, cdr): car.next = cdr

Though I'd be tempted to make it a method of ListNode:

   def cons(self, cdr): self.next = cdr

You build a list from the back to the front like so:

    LinkedList = cons(ListNode(1), cons(ListNode(2), ListNode(3)))

or

    LinkedList = ListNode(1).cons(ListNode(2).cons(ListNode(3)))

(this is heavily influenced by LISP; you might prever a different
abstraction) and then you'd iterate over the list like so:

     item = LinkedList
     while item:
         Process(item.data)
         item = item.next

Compared to a C implementation, which would look like:

     struct ListNode {
         struct ListNode *next ;
         void *data ;
     } ;

and an iteration like so:

    struct ListNode *item ;
    item = &LinkedList ;
    while (item) {
          Process(item->data) ;
          item = item->next ;
    }

I won't get into the details of building a list in C. It's just too
ugly.

Sorting is generally done on arrays, though there are methods that use
lists, etc. You can study those just fine in Python. However, those
are mostly of academic interest. Any computing platform worth
mentioning will have a good, general-purpose sort facility
available. Using that is certainly faster than writing and debugging
your own.

     <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list