I want to learn a new language...

Andrew McNabb amcnabb at mcnabbs.org
Wed Feb 14 15:48:14 MST 2007


On Wed, Feb 14, 2007 at 02:39:58PM -0700, Bryan Sant wrote:
> On 2/14/07, Daniel C. <dcrookston at gmail.com> wrote:
> >Python, obviously.
> 
> Why Python?  Why not Ruby or Perl instead?  What does Python have that
> they don't?

Why Python?  It's the only language I've ever used that I like more the
more I use it.  Preferences are subjective, and so are the reasons for
them.  Let me ramble a bit to give you some of what I like.  If I had
more time, I'd be more organized and succinct in my response.

1.  When I started using Python, I hated the idea of using whitespace
for syntax.  I endured this weakness (as I saw it).  After a few weeks,
my perspective changed completely.  We all use whitespace anyway.  When
the compiler cares, you get consistency and cleanness for free.  On at
least two occasions, I've spent hours trying to track down a bug of the
following form in Perl or C:

if (condition)
    x = 1
    y = 2

The code worked when it was:

if (condition)
    x = 1

and my eye just didn't see what was wrong when I added "y = 2".  I
haven't made this mistake in several years because I can't forget the
pain.  Python code looks really clean to me because of the way it uses
whitespace.


2.  Python is very dynamic.  Docstrings are amazing: any object can give
you its documentation on the fly.  Functions and classes are first class
objects.  You can ask a function how many parameters it takes.  You can
make things with very dynamic behavior.  For example, I have a plot
library which writes Gnuplot files.

Here are two different examples of code that uses the library to plot
some basic functions:

def f(x):
    return x**2
def g(x, y):
    return x**2 + y**2

# This opens up a Gnuplot window with a 2-D plot:
p = Plot(title='Test Function')
p.append(Function(f))
p.show()

# This opens up a Gnuplot window with a 3-D plot:
p = Plot(title='Test Function')
p.append(Function(g))
p.show()


3.  Python has a few high level structures that it makes very useable:
lists, dictionaries, and tuples.  In my experience, these constructs are
very practical.  For example, if I want to open a file and read in a
list of floating point numbers, with one number per line, it looks like
this:

lst = [float(line.strip()) for line in open('/path/to/file')]

In my opinion, this is incredibly readable, and it looks the same on the
screen as it was in my head before I wrote it down.

I can even take the squares of the list:

lst2 = [x**2 for x in lst]

These might look like dumb, simple examples, but the code I write every
day is full of similarly dumb and simple lines of code.  It's easy to
write, and it's easy to understand when I look at it later.


4.  I find Python to be clean and consistent all the way through, or at
least as much as any programming language can be.  Certain themes show
up in many different places, such as dictionaries.  A namespace is just
a dictionary, and defining a class is really just creating a dictionary
mapping names to attributes and methods.

One note: I originally was bothered that you write len(lst) instead of
lst.len().  However, in Python, len() is conceptually an operator.  I
write len(lst) instead of lst.len() just as I write "-x" instead of
"x.minus()".  This is an example of something that I like once I get
used to it.  The warts in other languages tend to bother me more and
more over time.

Sure, Python isn't perfect.  But it's so simple that I always feel like
I know where the imperfections are, and I write code without worrying
about them.


Anyway, those are just a few of those thoughts.  Don't tear them apart
because I didn't try to write some proof of why Python is the greatest
thing in the world or something.  This is just an attempt to identify a
few of the reasons why I seem to have more fun in Python than in most
other languages.  And honestly, there are a lot of other great languages
out there, and preferences really are subjective.

-- 
Andrew McNabb
http://www.mcnabbs.org/andrew/
PGP Fingerprint: 8A17 B57C 6879 1863 DE55  8012 AB4D 6098 8826 6868
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 186 bytes
Desc: not available
Url : http://plug.org/pipermail/plug/attachments/20070214/20b8a962/attachment.bin 


More information about the PLUG mailing list