First Look at Python

When the programming language Python came out while it seemed like an interesting idea, I didn’t really see the point. I already know Perl, PHP and C (plus a few others) why learn another language? So until recently, I didn’t

Recent frustrations with PHP and how “loose” it is with things like variables made me have another look at Python and what it could do for me.  As just learning a language by itself it pretty boring I decided to write some small programs in the language and used the framework Turbogears to do it.

First of all, learning the basics of python from perl or PHP is dead simple. The idea of what a language looks like is not too different between these three so it was a snap to write simple stuff in python.  So, what does yet-another language give me?

  • Variables must be defined before being used, but assigning them defines them too.  This is a nice compromise between PHP where you can check undefined variables and C where it all must be defined first, or else.
  • try and except. For code that might fail, use try and then catch the error in except.  We’ve all been guilty of using the @ symbol in PHP to supress stupid error messages, this does it correctly.
  • No PHP extract() or compact(). Actually just no extract function is good enough. That function is pure evil.
  • Sqlalchemy – A proper “you look after the database and ill play with objects” database extraction layer thingamy.  I don’t want to play with databases just let me write code and thats what sqlalchemy does well.
  • Testing – Turbogears uses nosetests which does functional and unit testing. I do get some annoying artifacts based upon testing but generally its all good.
  • Python has help() everywhere and pydoc on the command line. Not sure what variable foo can do? Just type “help(foo)” or even stick it in your script.
  • paster! You can run a development site off the command-line using a simple sqlite database. Don’t like it, dump the DB and reinstall it with one command.

It’s not one-way though, there have been some challenges.  Quite likely these are more my deficiencies than the languages.

  • Error messages take up a whole screen and often make it difficult to isolate. It’s not quite 10-screens-full ala Eiffel but sometimes the error messages tell you something is going wrong, but not what. Mess up your object versus database table references and you get something rather obscure.
  • When is an integer not an integer? The answer is, sometimes. Sometimes they work, sometimes they break. It’s probably more to do with what pysnmp is doing with them.
  • Documentation – some is great, some is downright misleading. It comes down to the writer of the documentation for modules. At least it doesn’t have ‘well get round to it’ like some functions in PHP on their website. Documents that say you can directly iterate dictionaries were another dead-end I wandered down. If you are stuck here, add a .items() at the end of it.
  • Deployment. It’s not just a matter of taring the files up and sticking them into /var/www/sitename and you’re done.  There are probably good reasons why, but its just annoying.

Despite the minor problems, python really does give you much much more than PHP and some more than Perl.  For me and what I am intending on writing it is worth learning yet another language.

Enhanced by Zemanta

9 thoughts on “First Look at Python

  1. @extract/compact: well, there is locals() and globals() — not that you’d need them often, but they are at times useful, e.g. when creating wrapper calls around something you know about in a descriptive data structure.
    usage: locals().update({“spam”:”eggs”}); print spam # prints “eggs”

  2. “””Documents that say you can directly iterate dictionaries were another dead-end I wandered down. If you are stuck here, add a .items() at the end of it.”””

    iterable as in

    for i in mydict:
    print my

    welcome to the python world 🙂

  3. One thing I found that made learning Python tremendously easy was a fancy py interpreter called IPython. I remember trying it the first time and hating it and going back to the normal py shell. Then I gave it another shot and fell in love.

    I know you can setup the standard py shell to have some of the features of IPython (tab completion, etc), but I’d rather install IPython.

  4. your last point is moot since it might be framework dependent, check out (it lets you easily pack your “webapp”)

    also, if you follow and use wsgi regardless of framework it may come down to dropping a dir with your stuff in it

    i hope this info helps you. enjoy

  5. Python is pretty strongly typed, so integers are always integers. Your code is almost certainly getting a value with a different type than it expects.

    Dictionaries are iterable, but they iterate over their keys, not their keys and values.

    1. I’ve seen places where they try
      for key,value in mydict:

      And sometimes this works and sometimes it doesn’t. Using items() fixes this. It’s just one of the gotchas. Congratulations on being the first commenter using IPv6 (that I’ve seen anyhow).

    2. This is the error message:
      pyasn1.error.PyAsn1Error: Component type error Integer() vs Integer(3)

      It’s not a python problem, its a pyasn1 problem. Where I saw it is when you want to SNMP walk a table and you want to limit the queries. So you first run a snmp get to get table length and stick that value into your second “get me the table” query as the row count.

  6. I would advise you to also take a look in Ruby, which shares a lot of the benefits of Python. Then, you may decide better what language to go with…

Comments are closed.