Switching from PHP to Python/Django
08 Jun 2009
5 comments
For the past two years Iʼve been a PHP programmer. I learned bit by bit by just typing away with the language. Trial and error eventually made me a good PHP programmer who was able to develop a complete framework for the company that I used to work with. About a year ago, thanks to my colleague Petar, I came in touch with Python. Learning a new programming language took some time, but with the help of some books I became fluent in programming with it. The following are my experiences from switching from PHP to Python.
In combination with Python we used the Django to develop web applications. A few books that were really helpfull for learning the language and the framework were the following:
-
Djangobook: A nice introduction to the Web Framework which motivates you to learn more about Python. It's also available for free on the internet.
-
Pro Django: The next step in understanding Django. More about decoupling applications and advanced uses of Django.
-
Expert Python Programming: Well written book which teaches you about Test Driven Development, Design Patterns and Documentation.
But the real question is why switch from PHP to Python? Below a few reasons which convinced myself to switch, not only from a programmers view but also interesting for the client.
Syntax
Syntax is literally what you type in the script. The real difference here is that Python resembles an English sentence. The code looks arranged. Another feature of Python code is indentation. Where PHP uses curly brackets for determining statements, Python uses indentation. In doing so this makes the script more readable.
Performance
Another reason to go for Python is that itʼs performance is better then PHP. This is also because the server backend can make use of a fast module called mod_wsgi. This can make a big difference for large scaled applications.
Re-usability
According to the DRY (Donʼt Repeat Yourself) principle, the focus of Django applications lies on reusability. The community of Python is very big and because of its focus on DRY you donʼt have to develop everything all over again. A lot of the already available applications can directly be plugged in your project with two lines of code.
Although te PHP community is also a big one, I don't think the quality of code is comparable with that of Python. PHP is a very nice language to start your programming career but for those who want to take the next step in code quality and usage of techniques like TDD and DRY, I truly believe Python is an better choice.
But if Python is really better why doesnʼt every company develop in Python? I can think of a few reasons why..
Hosting
Python has to be installed on the server just like PHP. Not every hosting company is prepared to do so and this has limitations on choosing a hosting company. While more and more hosting company offer support for Python (thanks to mod_wsgi), PHP is far more supported.
Investment
Every company has invested over the years to develop in a certain language. Python might be the better language to go for, but it's still is an investment in every employee to learn Python. Therefore not all companies are prepared to make such an investment.
The biggest reason to choose Python
Not mentioned yet, one reason is most important why you should try Python and Django to create your website's. It's a hell of a lot of fun to code again!
Next article Prev article
5 Comments
David
I'm looking forward to the day when I will have more time to invest in looking into Ruby and Python. I am a PHP dev and I have always wondered about large scale performance across high-level languages. Unfortunately, the only tests that are ever run seem to be semi-useless like the one you mentioned. There must be something out there that shows a full MVC system at work.
Petar Radosevic
It is very hard to create valid speed benchmarks between languages ([benchmark game][1] comes close). But this isn't necessary for Python and PHP when developing web-applications. Both languages offer sufficient speed. When speed is necessary, it's better to fix bottlenecks in low-level languages like C, Haskell, Erlang, etc..
[1]: http://shootout.alioth.debian.org/u32q/benchmark.php?test...
The most important thing is which language gives you the most productivity and code quality. For me, Python is a clear winner in this field. For you this could be different.
Pete Loggie
I started doing web development with PHP, and after finishing my first site I started playing with Python. Then I tried the Django framework. There's no contest: any personal websites I make from now on will be done using Python and Django.
Unfortunately, I make a living these days writing PHP code. Maybe I'll convince them to switch languages one day...
Truman
I agree to all comments and the author subject because this is your rich personal experience.
But, I think that python OOP is not so good and we need good books on python to really show clarity on use of OOP, methods and functions. As far as web applications are concerned, I not overly worried about shared hosts as there are enuf hosts now to put up mod-python.
Cheers, Truman
php/python
I Agree - I also used to code in PHP but a year ago or so I moved to Python. Besides the advantages mentioned in your post I can say that writing websites/anything in a new language is just a lot more fun. So if someone is tired of writing in the same language all the time, it's time to try something new - I recommend python + django.