3 ms·
I tried to build a solid web platform on PHP and Zend Framework for a few years. It proved very difficult. PHP doesn't provide much structure to build on. On th
by kennu 17y ago
I tried to build a solid web platform on PHP and Zend Framework for a few years. It proved very difficult. PHP doesn't provide much structure to build on. On the other hand ZF is overkill and slow with its massive class hierarchies and complex autoloading systems. And ZF's data models are a joke: they're just simple SQL table wrappers.
After moving to Python and Django, everything changed. Now there is a clear, simple structure for every application, defined by the framework itself. There is a good balance between models, views and templates, because Django enforces templates to be simple. There is no weird magic like ZF's autoloading or CodeIgniter's library loading, just basic Python imports.
It's really cool that whether you download a third-party Django app or use your own, you always have the familiar app structure of xxx.models, xxx.views, xxx.forms, xxx.admin and so on. This makes it really easy to understand how any code works.
I did go through a learning curve. First I tried to tweak Django to my own ideas of a good architecture. But slowly I realized that it actually makes sense to follow the conventions to the detail. Especially when you have a team of people that needs to follow the same principles, in order to understand and reuse each other's code.
I also used Python for several years as a shell scripting language before building real websites with it. Looking back, some of the code is pretty horrible and un-pythonic, more resembling Java or PHP, and not using Python's features properly.
So anybody new to Python should expect to go through a period of unlearning their old programming habits. Your code will shrink from the old 10-line-for-loops into 1-liners, you won't write much of your own classes any more, and eventually you'll start metaprogramming with decorators and metaclasses, to get rid of 90% of the remaining (boilerplate) code you thought you needed. :-)
When you reach Zen, you will no longer have to code. Instead you will declare your Django applications as data models, admin definitions, URL mappings, view decorators, HTML templates, and just a few lines of logic in the view functions.
- brehaut 17y agoIf you start using metaclasses its likely more experienced pythonista's will look down their nose at you and say "probably shouldn't have done it that way", largely because they often turn out to be magic for magics sake. I believe the rule is, if you don't know when to not use metaclasses, you shouldn't be using them at all.
- kennu 17y agoI agree. I think Django's model system is a good example of when metaclasses can help a lot. It's essentially a domain-specific language that overcomes some of Python's limitations, such as remembering the order in which class attributes were defined. I have noticed that the same sometimes applies to __getattr__ and similar Python features. You easily shoot yourself in the foot unless you are very careful. So all that stuff is best left to carefully debugged and maintained libraries, and out of everyday quickly-written application code.