Python is Weird

September 20, 2006

I've heard some good things about the Django framework. One thing that as me interested in Django is that the way it works in general is you define your domain model in code, in Python objects, and Django figures out how to build a database for you. Conceptually, I like this idea better than Ruby on Rails, where you define your domain model in the database, and then rails generates objects for you at runtime based on the database metadata. Also, you run it my installing mod_python in apache. Again, compared to rails with apache, FCGI/SCGI, lighttpd, mongrel, etc..., I don't know, let's just say I have found the process of setting up a rails production web/app server challenging. I haven't done it for Django yet, we'll see. Should be a small memory footprint too, making it easy to run multiple apps in my 192MB virtual server webhost.

But one thing I want to comment on now is that I find Python to be weird. Maybe this is because I have done most of my programming in Java, but when I started to learn Ruby, everything in Ruby just made sense, without Java's stupidity, such as NullPointerExceptions and primatives. I've soured on rails recently, but I miss Ruby. The language is succinct and powerful, it almost feels like writing psuedo code, except that it actually works.

Anyway, let me just give a few examples of python weirdness. But before I do, let me say this. I don't know python well at all. Consider these statements to be my first impressions of Python. I'm in no way stating that python is poor language due to these oddities, they just strike me as odd and I feel the need to express it. So, on with the weirdness:

So to get started, I started reading the Django install guide. I noticed I needed python 2.3 or higher. Fair enough, so python 2.5 is the latest, so I installed that.

The next requirement is for MySQLdb. It says it works with python 2.3-2.4, doesn't say anything about 2.5. I decide to install python 2.4 to be safe. So just deciding which version of python to use is a little bit of a struggle.

So now I have python 2.4 and MySQLdb. I want to try and do something simple to verify it is working. I find an example in the docs:

import MySQLdb
db=MySQLdb.connect(passwd="moonpie",db="thangs")

c=db.cursor()
max_price=5
c.execute("""SELECT spam, eggs, sausage FROM breakfast
          WHERE price < %s""", (max_price,))

What's with the 3 quotes? Anyway, let's give it a try. First, I add c:python24in to my path, fire up a command prompt, and enter "python" in hopes of getting an interactive shell. Python isn't found. Why not, because the python executables aren't in c:python24in, they are in c:python24. Ok fine, I fix my path, but I decide to use the IDLE GUI instead. I try to enter a query, but I make a typo. I press the up arrow, but it doesn't bring up my previous command. I re-type it and get it right this time. The query is:

select count(*) from users

This returns 9 when I execute the query in mysql. What does Python return?

1L

1L? What is that? Again, I am naive when it comes to python, this is not a fair criticism of Python, I have done very little to learn the language at this point, I just trying to illustrate a point. I know Java well. When I started learning Ruby, from the start, everything just worked as you expected it would. As I look into Python, I find one oddity after another. I'm sure if you know Python well you will consider many of the things in Java odd, so I'm just going to consider all of these things first impressions and move on to learn more about the language and give it a fair chance. But Python is still Weird.

Posted in Technology | Tags Django, Python, Ruby, Java, Rails

Comments

1.

The Rails equivalent of Django's models would be migrations (And models, for custom functions and the like.). As for Python, triple quotes delimit a string that covers multiple lines. I believe the query returned 1L because there was one result set, and t

# Posted By David Sissitka on Thursday, September 21 2006 at 12:10 AM

2.

Hey Paul -- gald to see you venturing into python :-)

Some notes:

1) Python 2.5 was released literally a few days ago, thus no mention of it in docs.

2) The use of three quotes is for multi-line quoting. A new line inside a single quote (either " or ')

# Posted By Duncan McGreggor on Thursday, September 21 2006 at 4:15 AM

3.

I'd start with "Learning Python" or "Dive Into Python" for easing yourself into the language.

As the others have pointed out, the Django snippet assumes knowledge of tuples, multi-line strings and long integer syntax. It would be like meeting Ruby for t

# Posted By Brandon Corfman on Thursday, September 21 2006 at 8:33 AM

4.

Hey guys, thanks for the feedback.

David:

- Migrations aren't actual objects that you use. Migrations are simply SQL DDL statements written in Ruby to abstract the database specific syntax. They also provide a way to keep track of individual chan

# Posted By Paul on Thursday, September 21 2006 at 10:03 AM

5.

I wasn't very clear about the whole Django models thing. There really isn't a one to one conversion, but if you piece together a few of the features present in Rails you can get pretty close. I'm new to Python myself, but it looks like 1L is a boolean val

# Posted By David Sissitka on Thursday, September 21 2006 at 12:49 PM

6.

As for editors, I use Komodo IDE (See: http://www.activestate.com/). It gets bonus points for supporting Ruby.

# Posted By David Sissitka on Thursday, September 21 2006 at 12:57 PM

7.

David, the 1L may *represent* a boolean (I have no idea what the mysql lib is doing; I can't remember the last time I used mysql or the python bindings for it... long live efficient ORMs), but it really is a long int :-)

Paul, I'm hardly the person to as

# Posted By Duncan McGreggor on Thursday, September 21 2006 at 6:20 PM

8.

Duncan,

I understand what you are saying about IDEs. The things I absolutely need out of an IDE/Editor is:

1. The ability to run unit tests and see the results from within the editor. You do write unit tests in python/django apps, right? :)

2.

# Posted By Paul on Thursday, September 21 2006 at 6:38 PM

9.

Hey Paul,

1. Do you know about doctests in python? They are freaking awesome, and if I'm not doing Twisted python work, I put all my unit tests in doctests -- python code embedded in doc strings (you can copy and paste from the python interpreter, as it

# Posted By Duncan McGreggor on Thursday, September 21 2006 at 7:17 PM

10.

Currently you can try out Komodo 4.0 for free since it's in alpha (but still stable -- I've used it):
http://activestate.com/store/beta/register.plex?id=komodo

Wingware also has a free 30-day trial:
http://wingware.com/wingide/trial

A review of 6 Python

# Posted By Brandon Corfman on Friday, September 22 2006 at 11:06 AM

11.

P.S. I just read more of your blog and realized you live close to Baltimore. I'm in NE Maryland, in Havre de Grace. Small world. :)

# Posted By Brandon Corfman on Friday, September 22 2006 at 11:11 AM

12.

`c.execute(..)` returns 1L which is number of rows returned by SQL statement (this is specific for MySQLdb library). If You would like to get value of SQL `count(*)` You must use:

row = c.fetchone()
count = row[0]

(This value is handy, i

# Posted By Lukasz on Monday, September 25 2006 at 8:52 AM

13.

Python has some neat class type stuff. For example, a unicode string would be u"this is a unicode string." Similarly, 1L implies 1 (like, the number) returned as a Long. It's Django trying to make sure nothing breaks when you get an ID of 49582044506 and try to int() that sucker.

Best,
Flip

# Posted By Flip on Tuesday, February 6 2007 at 10:30 AM

Comments Disabled