Python: Taming the serpent.

Python is a high-level programming language created by Guido van Rossum, a dutch computer programmer. He started out with a set of design goals for Python that allowed Python to be easy, intuitive, easily understandable, open source and had a very short learning curve.

Python was born in 1991 and was conceived by Van Rossum in the late 80”s, so per say it is not a very new language. So what makes it so special and more importantly why all the ruckus? Well, because of its simplicity Python is quickly becoming one of the leading programming languages and language of choice among developers today.

I am a staunch believer that no one programming language is better than the other. Forget about the language shoot-outs and the bench-mark tests created by “so called” experts. They are at best inconclusive and most of the time, flawed. Each language is like a tool and each is designed to address a particular problem or sets of problems. As an experienced developer you must know the merits and demerits of every language (or at least most of the popular ones). Don’t be shy to learn a new language, you probably do yourself a world of good by learning three, four or even more languages.

While C++ and Java (and to some extent C#) are the unequivocal barons of the programming world, Python’s attitude is very different. Its likes to be the short sweet comedian with its unconventional syntax and ridiculously boyish code layout. Let the looks not fool you though, Python is a very powerful language and can easily go head to head with any of the above mentioned trio. Python has all the power the three can yield and yet allows you to be simple in every sense of the way.

Another very important aspect of Python is, it is very easy to learn as compared to C++ or Java. With a good resource, you can pick it up within a matter of days. The syntax is very easy to understand and almost seems natural. I have seen people, who have had no programming backgrounds earlier, pick up basic Python skills in 2 or 3 days. Compare to that, learning C++ could take many weeks. C++ is an old language and has some quirks like, poor (or nonexistant) memory management, pointers, undefined behaviour, no garbage collection and many more. Most modern languages including Python solve these and many other issues.

For me, coming from a C++ background, Python was like a blessing in disguise. Make no mistake about it, it is not a perfect language. No language is. But it has its place. Most C++ veterans I have worked with and who have used Python swear by its merits. One senior colleague of mine even went as far as saying that Python was like a C++ scripting language. Well, it is not, but he was speaking metaphorically. What he meant was, Python has most of the merits of C++ and almost none of its short-comings. I still work 90% of the time on C++ (or is it 95%), and I am not putting down C++ in any way. C++ will continue to be popular and I am sure I will be working in C++ for quite a while more (since I am an game engine programmer, and C++ will probably stick with me longer). As I said, each language has its merits and demerits.

Python offer higher productivity than C/C++, meaning you can write, debug, test programs faster in Python than you can in C++. It offers all the new programming language features like modules, classes, exceptions, very high level dynamic data types, dynamic typing and automatic garbage collection. Yes! Python is slower than C/C++ per say. But slow is a very relative term. A badly designed program in C can be slower than a correctly designed program in Python or any other language. Theoretically a program written in assembly/machine language should be faster than any program, even when compared to a compiled language program like C++. You don”t see many programmers (even game developers) write codes in assembly, do you? Why? Maintaining an assembly program is a nightmare. Case and point to C++. As programs get larger, complexity increases exponentially. Python by design has a very clear syntax which goes a long way in avoiding complexity.

So what do you require to get started with Python? Well first download and install Python. The 2 places that I recommend are The Python official site and Activestate. I am a little partial on Activestate because their distribution involves a complete package including comprehensive documentation and an IDE (under Windows). After that you need a place to type in Python code. I personally used only a notepad (under Windows) or emacs (when using under Linux) when I began. Today there are a myriad of tools/IDEs available. The Python community is large and had done an excellent job at coming up with some very good tools. Strictly, you don’t need any tools. However most IDEs do offer productivity boosts. My current favorite is PythonWin. Many people also prefer SPE. What you use is up to you really. As a beginner I would recommend Activestate’s distribution and directly using the command line and/or notepad/emacs.

The Python documentation comes in with all the tutorials you will ever need to get started with the language. Taking the tutorial one step at a time is all that you need to do. The Activestate’s distribution also has 2 excellent books bundled in plus some fair extra bit of Python goodies in the form of documentation. The Python tutorials found in the documentation are geared towards an absolute beginner. If you are already a programmer working on (insert your favorite language here), then picking up Python is just a matter of reading through the first few chapters. For beginners, it is recommended that you read through all the chapters one by one. Examples start with simple calculus and slowly move on to more advanced stuff like arrays, tuples, sequences. Within a short time you will be proficient in writing complex scripts rather easily.

Python is object-oriented, but in sprite a lot less complicated than C++ or Java. Python’s class mechanism adds classes to the language with a minimum of new syntax and semantics. Very easy for a newbie to pickup, and often appreciated by veterans. All class members (including the data members) are public, and all member functions are virtual. You could argue that this violates encapsulation and to some extent is an over utilization of polymorphism, you are correct, it is. What it does, is makes things much simpler. Instead of a newbie trying to figure out how polymorphism works or why his protected functions are not accessible, Python empowers him to concentrate on actual writing of his program. Maybe you don’t agree with me, but this is very much in line with Python philosophy, short and sweet.

Next we move on to GUIs. GUIs are the foundation of computer programs these days. A good GUI can make all the difference and can literally make or break your program, unless of course you are strictly writing server-side code or device drivers. Python comes bundled with Tkinter (Tk for short) and is Python’s de-facto standard GUI. Tkinter is Python’s interface to Tcl/Tk. It is a lightweight toolkit which offers basic functionality through a set of widgets. It is very easy to learn and understand, and if you are new to GUI programming, you will probably find Tkinter a lot easier to pick up than most other toolkits. Although tkinter is very easy to learn, it falls short for complex GUIs. Tkinter is by design small and lightweight (adhering to python philosophy), but if your GUI is complex, you end up writing horribly complex code to get in all the functionality. For more complex GUIs there is an excellent toolkit/framework called wxPython. wxPython is actually a Python binding for the popular (and comprehensive) GUI package calledwxWidgets. The package offers nearly complete functionality w.r.t modern GUI design. It has a huge collection of widgets and an equally large community behind it. There are also bindings to other toolkits please refer below.

Being a game developer, I just could not resist not having something related to game development in this blog. So here it is, Python has bindings for OpenGL, the defacto industry standard for 3D development called PyOpenGL. OpenGL itself is limited to 3D graphics, what most game developers or, rather game development needs is an access to complete game development system. That is exactly what is offered by pygame. pygame is actually a Python binding for SDL and OpenGL. It is mature enough to be used in game development, but still has some way to go before it can be considered main stream. To be fair, I have only played with it for a while and I am no expert with it. I encourage you to try, and if you are a budding game developer or a newbie to game development, then I strongly suggest you take a tour of the library.

Before I conclude this post there is one more aspect of Python that I want to talk about and that is Python as scripting language for your program. Python can offer strong extensibility features to your program/library written in C/C++. Even if your library itself is written in C/C++, having python bindings to it can boost productivity significantly. Even with game engines you don”t need to have all your code in C++. I fact doing so can be a real pain to deal with. C++ is not the friendliest of languages where as Python fills in the gaps left over by C++ nicely. To be fair, Python was never meant to be a embeddable scripting language. It is designed to be a high level language for rapid application development. There are other languages like Lua and/or AngelScript (also many more) that are more geared towards the embeddable language design philosophy. So go back a few sentences and read what I said; I said “Python offers strong extensibility features.” Meaning you can write the core of your program/library or parts of code that are time-critical in C/C++ and then have python bindings for them so that they can be accessed via python. You can write bindings for C/C++ code using SWIG or using Boost.Python.

As a parting note I would like to mention StacklessStackless is not a library or a module but a modified version of Python that allows the use of light-weight threads called microthreads. Microthreads allow for all benefits that threads have to offer at the same time avoiding complexity and overheads associated with conventional thread based programming. Using any kind of threading implementation in your program can be a challenge, so Stackless can find limited use with conventional programmers. Microthreads are mainly used in AI (Artificial Intelligence) systems for game engines to allow a more natural module design. They are similar to coroutines in Lua.

The truth is that there are many (many) ways to implement advanced functionality with Python. Though it is debatable whether Python offers a pure functional programming paradigm, it does allow for a “functional style” (for lack of a better word) program to be written. Another advanced concept is concurrency. Concurrency mechanisms are often difficult to implement with Object-Oriented style of programming. It involves a whole new set of design fundamentals and issues. Though my knowledge in these areas is limited, I have read a paper on this very topic a few years back. It can be found here. You are free to draw your own conclusions on these subjects.

Last, for some real world implementations and applications. It is no secret that Google uses Python extensively to power it’s engine and services. Another good example is an open source 3D modeling program called Blender. There are many more, but I leave it to you as an exercise.

SOME EXTRA GUI LIBRARIES FOR PYTHON.

PyQt
PyGTK

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.